B站的每个视频都有对应的时长,点赞数,投币数,收藏数,弹幕条数,评论人数,以及包括视频标题,封面等等信息,还有很多有关视频和up主的相关信息。现在我们试着用python来爬取一些有趣的信息。
import urllib3
import requests
import json
from urllib import request
bv=input('please input a bvid:')
url='http://api.bilibili.com/x/web-interface/view?bvid='+bv
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.129 Safari/537.36"
} #请求头,模拟浏览器的运行
urllib3.disable_warnings() #从urllib3中消除警告
response = requests.get(url,headers=headers)
content = json.loads(response.text)
# 获取到的是str字符串 需要解析成json数据
# print(response.content.decode('utf-8'))
statue_code = content.get('code')
#print(statue_code)
if statue_code == 0:
cid=content['data']['cid']
duration=content['data']['duration']
view=content['data']['stat']['view']
danmaku=content['data']['stat']['danmaku']
reply=content['data']['stat']['reply']
favorite=content['data']['stat']['favorite']
coin=content['data']['stat']['coin']
share=content['data']['stat']['share']
like=content['data']['stat']['share']
data=content['data']['pic'] #封面信息
name='fengmian.jpg'
request.urlretrieve(data,filename=name)
print('cid:',cid)
print('duration(时长):',duration)
print('view(观看次数):',view)
print('danmaku(弹幕条数):',danmaku)
print('reply(评论条数):',reply)
print('favorite(收藏人数):',favorite)
print('coin(投币数):',coin)
print('share(分享):',share)
print('like(点赞人数):',like)
please input a bvid:BV1JK411p7pd
cid: 197603144
duration(时长): 78
view(观看次数): 649699
danmaku(弹幕条数): 1419
reply(评论条数): 1666
favorite(收藏人数): 22566
coin(投币数): 38282
share(分享): 7679
like(点赞人数): 7679
然后在程序的同一目录下会出现对应视频的封面(awsl,为小姐姐打call!!!)
至于视频里的文件中的信息是如何排列的,json文件中具体的的数据格式的问题,需要参考下面的文章,里面详细的列举了B站视频的各种信息的存储位置和作用,希望对大家有所帮助。
https://github.com/SocialSisterYi/bilibili-API-collect/blob/master/video/info.md
更多文章:
python进行B站av号和bv号的转换
网友评论