>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
因为太过于简单,所有就只写了基本的流程
网页分析
进入一个视频,然后抓包
通过filter过滤带有'list'的地址
弹幕在以下地址中
https://api.bilibili.com/x/v1/dm/list.so?oid=193943133
https://api.bilibili.com/x/v1/dm/list.so?oid=194941323(另一个视频的弹幕地址,为了区别哪些参数变化了)
oid参数在变化,即一个视频对应一个oid
获取oid
https://api.bilibili.com/x/player/pagelist?bvid=BV1T5411x7y3&jsonp=jsonp
该地址返回一个json字符串,
去掉&jsonp=jsonp后返回的数据不变
该地址返回的数据如下:
{
code: 0,
message: "0",
ttl: 1,
data: [
{
cid: 193943133,
page: 1,
from: "vupload",
part: "咖喱爱情",
duration: 221,
vid: "",
weblink: "",
dimension: {
3840,
height: 2160,
rotate: 0
}
}
]
}
可以发现oid和cid是一样的
而https://api.bilibili.com/x/player/pagelist?bvid=BV1T5411x7y3中的bvid后面的值为视频的编号
可以发现在https://www.bilibili.com/video/BV1T5411x7y3,的值符合
总结,通过bvid=BV1T5411x7y3构造https://api.bilibili.com/x/player/pagelist?bvid=BV1T5411x7y3
获得oid号(即cid),通过oid构造弹幕请求的地址:https://api.bilibili.com/x/v1/dm/list.so?oid=193943133
总结:当自己找不到数据入口的时候,不妨先去网上找一下思路,这个爬虫的难点是弹幕数据入口不清楚,只要找到就好了
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
import requests
import re
from lxml import etree
from pprint import pprint
bvid_url = 'https://www.bilibili.com/video/BV1T5411x7y3'
bvid = re.findall(r"video/(S+)", bvid_url, re.S)[0]
oid_url = "https://api.bilibili.com/x/player/pagelist?bvid={}".format(bvid)
headers = {
"User-Agent": 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36',
"referer": "https://www.bilibili.com/video/BV1T5411x7y3"
}
response = requests.get(oid_url, headers=headers).content.decode()
oid = re.findall(r'"cid":(.*?),', response, re.S)[0]
danmu_url = "https://api.bilibili.com/x/v1/dm/list.so?oid={}".format(oid)
response = requests.get(danmu_url,headers=headers).content
html = etree.HTML(response)
d_list = html.xpath("//d/text()")
pprint(d_list)