爬虫实战 - 如何爬取B站视频评论?
程序员文章站
2022-06-27 23:29:20
步骤(本次爬虫仅以一个视频为示例:链接)查找评论请求api解析URL去掉第一个和最后一个参数可得评论URL,即:https://api.bilibili.com/x/v2/reply?jsonp=jsonp&pn=1&type=1&oid=585286365&sort=2【其中pn是页码;sort控制排序顺序,1按时间排序,2按热度排序;oid代码视频编号】- 开始敲代码import requestsheader = {"User-Agent": "M...
步骤
(本次爬虫仅以一个视频为示例:链接)
- 查找评论请求api
- 解析URL
去掉第一个和最后一个参数可得评论URL,即:https://api.bilibili.com/x/v2/reply?jsonp=jsonp&pn=1&type=1&oid=585286365&sort=2
【其中pn是页码;sort控制排序顺序,1按时间排序,2按热度排序;oid代码视频编号】
- 开始敲代码
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)
本文地址:https://blog.csdn.net/Totoro1745/article/details/109908334