(本次爬虫仅以一个视频为示例:链接)
- 查找评论请求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)