文章总结: 这篇文章介绍了使用Python的requests和lxml库进行网页爬虫的基础知识,重点讲解了XPath语法的应用。文章通过爬取格言网站的实例,展示了如何分析网页结构、定位元素、提取内容,并最终编写出完整的爬虫脚本。作者提供了详细的代码示例,包括如何处理编码问题、如何提取特定标签内的文本内容、如何封装函数以及如何遍历多个链接并保存爬取的内容。这篇文章对于爬虫初学者来说是一个很好的入门教程,提供了实用的代码示例和清晰的步骤说明。
综合评分: 86
文章分类: 爬虫,WEB安全,安全工具
爬虫–xpath的基础使用
原创
小白鱼来了
Joker One Security
2025年7月30日 02:10
北京
使用的库
requestsfrom lxml import etree
案例
aHR0cHM6Ly93d3cueWpieXMuY29tL2xpemhpL2dleWFuLw==
爬取该url的格言标题内容
判断标题值
可以看到该处的标题是位于div标签下的h1标签的,而div标签的名称为class=”article”,即最终的xpath语法为div[@class=”article”]/h1/text()
同理该处标题的内容为div下的p标签,而div标签的名称为class=”content”,即最终的xpath语法为
div[@class=”content”]/p/text()
因为我们获取是他对应的内容,即我们需要使用text(),那么完整的伪代码为
title = html1.xpath('//div[@class="article"]/h1/text()') # 提取标题的XPathcontent = html1.xpath('//div[@class="content"]/p/text()') # 提取内容的XPath
在提取这些内容前,我们需要对url发起请求并使用etree规范标准的格式,那么完整的伪代码为
r1 = requests.get(url) #发起get请求r1.encoding = r1.apparent_encoding # 让获取的text内容不乱码html1 = etree.HTML(r1.text) # 实体化text内容方便后面xpath提取
根据以上的步骤成功编写出我们想要提取的值,继续完善成为完整的代码
r1 = requests.get(url)r1.encoding = r1.apparent_encodinghtml1 = etree.HTML(r1.text)title = html1.xpath('//div[@class="article"]/h1/text()') content = html1.xpath('//div[@class="content"]/p/text()')content = "".join(content)
功能实现
根据上述的步骤获取标题和内容的xpath后编写一套完整的脚本
import requestsfrom lxml import etreeurl = 'https://www.yjbys.com/lizhi/geyan/2351816.html'r1 = requests.get(url)r1.encoding = r1.apparent_encodinghtml1 = etree.HTML(r1.text)title = html1.xpath('//div[@class="article"]/h1/text()')content = html1.xpath('//div[@class="content"]/p/text()')content = "".join(content)print(title)print(content)
该单个的标题及对应内容提取代码完成
整个页面的爬取
根据上述的完整爬取代码我们可以将其构造为一个函数,函数伪代码为
def get_content(url): r1 = requests.get(url) r1.encoding = r1.apparent_encoding html1 = etree.HTML(r1.text) title = html1.xpath('//div[@class="article"]/h1/text()') # 修正提取标题的XPath content = html1.xpath('//div[@class="content"]/p/text()') content = "".join(content) return title,content
编写完函数后,我们回到url首页,去提取每个格言的链接
每个格言的链接位于div下的a标题,提取的xpath语法为div[@class=”newlist”]//a/@href 那么提取链接的伪代码为
links = html.xpath('//div[@class="newlist"]//a/@href')
组成完整的代码
index_url = "https://www.yjbys.com/lizhi/geyan/"r = requests.get(index_url)r.encoding = r.apparent_encodinghtml = etree.HTML(r.text)
网站的链接提取完了那我们就可利用for循环和函数去进行爬取了,for循环的伪代码
for link in links: title,content = get_content(link) with open(f"E:\\xpatch\\{title}.txt","w",encoding='utf-8') as f: f.write(f"{title} + '\n\n'") f.write(f"{content} + '\n\n'") print(f"正在下载---------{title}")
将每个爬取的格言标题单独保存成一个文档,综合以上步骤完成一个完整爬取的脚本
完善脚本
import requestsfrom lxml import etree
index_url = "https://www.yjbys.com/lizhi/geyan/"r = requests.get(index_url)r.encoding = r.apparent_encodinghtml = etree.HTML(r.text)links = html.xpath('//div[@class="newlist"]//a/@href')
def get_content(url): r1 = requests.get(url) r1.encoding = r1.apparent_encoding html1 = etree.HTML(r1.text) title = html1.xpath('//div[@class="article"]/h1/text()') # 修正提取标题的XPath content = html1.xpath('//div[@class="content"]/p/text()') content = "".join(content) return title,content
for link in links: title,content = get_content(link) with open(f"E:\\xpatch\\{title}.txt","w",encoding='utf-8') as f: f.write(f"{title} + '\n\n'") f.write(f"{content} + '\n\n'") print(f"正在下载---------{title}")
运行此代码
成功爬取付费内容
免责声明:
本文所载程序、技术方法仅面向合法合规的安全研究与教学场景,旨在提升网络安全防护能力,具有明确的技术研究属性。
任何单位或个人未经授权,将本文内容用于攻击、破坏等非法用途的,由此引发的全部法律责任、民事赔偿及连带责任,均由行为人独立承担,本站不承担任何连带责任。
本站内容均为技术交流与知识分享目的发布,若存在版权侵权或其他异议,请通过邮件联系处理,具体联系方式可点击页面上方的联系我。
本文转载自:Joker One Security 小白鱼来了《爬虫–xpath的基础使用》