admin管理员组文章数量:1026989
Xpath
Xpath–爬取哔哩哔哩排行榜数据
以前对爬取的数据做数据解析都是用我蹩脚的正则表达式,大家看了都直摇头,
然后我就学习了一下Xpath,感觉入门挺快的,没有之前想的那么复杂
这次我选择了哔哩哔哩的排行榜
目标URL:哔哩哔哩排行榜
先按F12查看一下我们需要爬取数据的结构情况
结构还是很清晰的,一目了然
那就开始写代码吧
首先导入我们会用到的包
# 这是Xpath需要用的
from lxml import etreeimport requests
以防万一,先伪装个信息头(headers)
在开发者模式里找到User-Agent、referer、cookie填到下面
headers = {'User-Agent': '','referer': '','cookie': ''
}
添加我们的链接进去
url = ''
开始获取网页数据和解析数据
response = requests.get(url, headers=headers, timeout=5)
# 判断是否响应成功
if (response.status_code) == 200:# 构造了一个XPath解析对象并对HTML文本进行自动修正。html = etree.HTML(response.text)# 标题title_list = html.xpath('//div[@class="info"]/a/text()')# 链接link_list = html.xpath('//div[@class="info"]/a/@href')# 总评分hot_list = html.xpath('//div[@class="pts"]/div/text()')# 播放量watch_list = html.xpath('//div[@class="detail"]/span[1]/text()')# 弹幕量barrage_list = html.xpath('//div[@class="detail"]/span[2]/text()')# 作者author_list = html.xpath('//div[@class="detail"]/a/span/text()')
如果使用Xpath,其实可以在F12(开发者工具)里按Ctrl+F调试,挺方便的。
输入Xpath表达式,就会自动匹配表达式(代码高亮),而且不需要很长、也不是晦涩难懂的表达式
然后把爬取到的数据组合起来,格式化一下,保存到文件里,我这里是保存到的D盘
- zip()用于将可迭代的对象作为参数,将对象中对应的元素打包成一个个元组,然后返回由这些元组组成的列表。
- .strip()去除前导和后导空格
for title, watch, barrage, author, hot, link in zip(title_list, watch_list, barrage_list, author_list, hot_list, link_list):line = ("{0}、{1}\n\t[👀{2}][📺{3}][🧑{4}][🔥{5}]\n\t👉http:{6}".format(title_list.index(title)+1, title, watch.strip(), barrage.strip(), author.strip(), hot, link))with open('D:/bilibili排行榜.txt', 'a+', encoding='utf-8') as file:file.writelines('\n'+line + '\n')print('收集完成')
以下是完整代码
from lxml import etree
import requestsheaders = {'User-Agent': '','referer': '','cookie': ''
}url = ''try:response = requests.get(url, headers=headers, timeout=5)if (response.status_code) == 200:# 构造了一个XPath解析对象并对HTML文本进行自动修正。html = etree.HTML(response.text)# div[contains(@class,"carousel-item")]# 标题title_list = html.xpath('//div[@class="info"]/a/text()')# 链接link_list = html.xpath('//div[@class="info"]/a/@href')# 总评分hot_list = html.xpath('//div[@class="pts"]/div/text()')# 播放量watch_list = html.xpath('//div[@class="detail"]/span[1]/text()')# 弹幕量barrage_list = html.xpath('//div[@class="detail"]/span[2]/text()')# 作者author_list = html.xpath('//div[@class="detail"]/a/span/text()')for title, watch, barrage, author, hot, link in zip(title_list, watch_list, barrage_list, author_list, hot_list, link_list):line = ("{0}、{1}\n\t[👀{2}][📺{3}][🧑{4}][🔥{5}]\n\t👉http:{6}".format(title_list.index(title)+1, title, watch.strip(), barrage.strip(), author.strip(), hot, link))with open('D:/bilibili排行榜.txt', 'a+', encoding='utf-8') as file:file.writelines('\n'+line + '\n')print('收集完成')except Exception as error:print(error)
来看一看结果
感觉还是可以的
到此结束
Xpath
Xpath–爬取哔哩哔哩排行榜数据
以前对爬取的数据做数据解析都是用我蹩脚的正则表达式,大家看了都直摇头,
然后我就学习了一下Xpath,感觉入门挺快的,没有之前想的那么复杂
这次我选择了哔哩哔哩的排行榜
目标URL:哔哩哔哩排行榜
先按F12查看一下我们需要爬取数据的结构情况
结构还是很清晰的,一目了然
那就开始写代码吧
首先导入我们会用到的包
# 这是Xpath需要用的
from lxml import etreeimport requests
以防万一,先伪装个信息头(headers)
在开发者模式里找到User-Agent、referer、cookie填到下面
headers = {'User-Agent': '','referer': '','cookie': ''
}
添加我们的链接进去
url = ''
开始获取网页数据和解析数据
response = requests.get(url, headers=headers, timeout=5)
# 判断是否响应成功
if (response.status_code) == 200:# 构造了一个XPath解析对象并对HTML文本进行自动修正。html = etree.HTML(response.text)# 标题title_list = html.xpath('//div[@class="info"]/a/text()')# 链接link_list = html.xpath('//div[@class="info"]/a/@href')# 总评分hot_list = html.xpath('//div[@class="pts"]/div/text()')# 播放量watch_list = html.xpath('//div[@class="detail"]/span[1]/text()')# 弹幕量barrage_list = html.xpath('//div[@class="detail"]/span[2]/text()')# 作者author_list = html.xpath('//div[@class="detail"]/a/span/text()')
如果使用Xpath,其实可以在F12(开发者工具)里按Ctrl+F调试,挺方便的。
输入Xpath表达式,就会自动匹配表达式(代码高亮),而且不需要很长、也不是晦涩难懂的表达式
然后把爬取到的数据组合起来,格式化一下,保存到文件里,我这里是保存到的D盘
- zip()用于将可迭代的对象作为参数,将对象中对应的元素打包成一个个元组,然后返回由这些元组组成的列表。
- .strip()去除前导和后导空格
for title, watch, barrage, author, hot, link in zip(title_list, watch_list, barrage_list, author_list, hot_list, link_list):line = ("{0}、{1}\n\t[👀{2}][📺{3}][🧑{4}][🔥{5}]\n\t👉http:{6}".format(title_list.index(title)+1, title, watch.strip(), barrage.strip(), author.strip(), hot, link))with open('D:/bilibili排行榜.txt', 'a+', encoding='utf-8') as file:file.writelines('\n'+line + '\n')print('收集完成')
以下是完整代码
from lxml import etree
import requestsheaders = {'User-Agent': '','referer': '','cookie': ''
}url = ''try:response = requests.get(url, headers=headers, timeout=5)if (response.status_code) == 200:# 构造了一个XPath解析对象并对HTML文本进行自动修正。html = etree.HTML(response.text)# div[contains(@class,"carousel-item")]# 标题title_list = html.xpath('//div[@class="info"]/a/text()')# 链接link_list = html.xpath('//div[@class="info"]/a/@href')# 总评分hot_list = html.xpath('//div[@class="pts"]/div/text()')# 播放量watch_list = html.xpath('//div[@class="detail"]/span[1]/text()')# 弹幕量barrage_list = html.xpath('//div[@class="detail"]/span[2]/text()')# 作者author_list = html.xpath('//div[@class="detail"]/a/span/text()')for title, watch, barrage, author, hot, link in zip(title_list, watch_list, barrage_list, author_list, hot_list, link_list):line = ("{0}、{1}\n\t[👀{2}][📺{3}][🧑{4}][🔥{5}]\n\t👉http:{6}".format(title_list.index(title)+1, title, watch.strip(), barrage.strip(), author.strip(), hot, link))with open('D:/bilibili排行榜.txt', 'a+', encoding='utf-8') as file:file.writelines('\n'+line + '\n')print('收集完成')except Exception as error:print(error)
来看一看结果
感觉还是可以的
到此结束
本文标签: xpath
版权声明:本文标题:Xpath 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/jiaocheng/1706778242a422681.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论