• 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏吧

爬虫实战 – 如何爬取B站视频评论?

互联网 diligentman 1周前 (11-22) 9次浏览

步骤

(本次爬虫仅以一个视频为示例:链接)

import requests

header = {"User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:22.0) Gecko/20100101 Firefox/22.0",
		  "Cookie": ""}
comments = []
original_url = "https://api.bilibili.com/x/v2/reply?jsonp=jsonp&type=1&oid=585286365&sort=2&pn="

for page in range(1, 39):   # 页码这里就简单处理了
    url = original_url + str(page)
    print(url)
    try:
        html = requests.get(url, headers=header)
        data = html.json()
        if data['data']['replies']:
            for i in data['data']['replies']:
                comments.append(i['content']['message'])
    except Exception as err:
        print(url)
        print(err)


喜欢 (0)