Xpath--爬取哔哩哔哩排行榜数据

Xpath–爬取哔哩哔哩排行榜数据

以前对爬取的数据做数据解析都是用我蹩脚的正则表达式,大家看了都直摇头,
然后我就学习了一下Xpath,感觉入门挺快的,没有之前想的那么复杂

这次我选择了哔哩哔哩的排行榜

目标URL:哔哩哔哩排行榜

先按F12查看一下我们需要爬取数据的结构情况

一目了然
结构还是很清晰的,一目了然

那就开始写代码吧

首先导入我们会用到的包

# 这是Xpath需要用的
from lxml import etree

import requests

以防万一,先伪装个信息头(headers)

在开发者模式里找到User-Agent、referer、cookie填到下面

headers = {
    'User-Agent': '',
    'referer': '',
    'cookie': ''
}

添加我们的链接进去

url = 'https://www.bilibili.com/v/popular/rank/all'

开始获取网页数据和解析数据

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盘

  1. zip()用于将可迭代的对象作为参数,将对象中对应的元素打包成一个个元组,然后返回由这些元组组成的列表。
  2. .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 requests

headers = {
    'User-Agent': '',
    'referer': '',
    'cookie': ''
}

url = 'https://www.bilibili.com/v/popular/rank/all'

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)

来看一看结果
在这里插入图片描述

感觉还是可以的

到此结束

  • 6
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值