根据cid获取哔哩哔哩弹幕

时间:2023-03-09 18:05:11
根据cid获取哔哩哔哩弹幕
def biliget(cid):
headers = {
"Accept": "*/*",
"Accept-Language": "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3",
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:55.0) Gecko/20100101 Firefox/55.0"
}
t=''
url='https://api.bilibili.com/x/v1/dm/list.so?oid='+str(cid)
body = requests.get(url, headers=headers).content
xbody = Selector(text=str(body, encoding='utf-8'))
lists = xbody.xpath("//d")
count = xbody.xpath("//maxlimit/text()").extract_first()
print(str(cid)+"共有%s条弹幕" % count)
for li in lists:
content = li.xpath("./text()").extract_first()
par = li.xpath("./@p").extract_first()
text=str(cid)+';'+str(content)+";"+str(par)+'\n'
t=t+text
return(t)