99网
您的当前位置:首页Xpath--爬取哔哩哔哩排行榜数据

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

来源:99网

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盘

        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)

来看一看结果

感觉还是可以的

到此结束

因篇幅问题不能全部显示,请点此查看更多更全内容