python3 根据视频BV号爬B站视频的评论消息和UP主的个人信息

简单写一个b站的爬虫

python3 根据视频BV号爬B站视频的评论消息和UP主的个人信息

代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import re
import json
import requests
import time, datetime
import prettytable
from textwrap import fill


def Input_BV(bv_id):
target_url_1 = "https://www.bilibili.com/video/"+str(bv_id)
headers = {
'Cookie': '1', #cookie可以随意设置
'User-Agent': 'Mozilla/5.0'
}
resp_1 = requests.get(url = target_url_1 )
jsons = json.loads(re.search(r"window\.__INITIAL_STATE__=(.*?);", resp_1.text).group(1))
#获取对应视频BV号的UP主的信息,即获取mid
mid = jsons['upData']['mid']
get_up_info(mid)
#获取对应视频BV号下的评论,此时aid=oid
oid = jsons['aid']
get_BV_comment(oid)

#获取对应视频BV号的UP主信息
def get_up_info(mid):
url = "https://api.bilibili.com/x/web-interface/card?article=true&mid=" + str(mid)
headers = {
'Cookie': '1', #cookie可以随意设置
'User-Agent': 'Mozilla/5.0'
}
resp = requests.get(url, headers=headers)
jsons = json.loads(resp.text)
#up的名字
up_name = jsons['data']['card']['name']
#up的性别
up_sex = jsons['data']['card']['sex']
#up的粉丝数
up_fans = jsons['data']['card']['fans']
#up的关注数
up_attention = jsons['data']['card']['attention']
#up的个性签名
up_sign = jsons['data']['card']['sign']
#up的投稿数量
up_archive_count = jsons['data']['archive_count']
#up的获赞数量
up_like_num = jsons['data']['like_num']

#使用prettytable美化数据输出
table = prettytable.PrettyTable()
#设置表头
table.field_names = ['up的姓名','up的性别','up的粉丝数','up的关注数','up的投稿数','up的获赞数','up的个性签名']
table.align['up的个性签名']='l' #个性签名左对齐
#将数据加入到表格
table.add_row([up_name,up_sex,str(up_fans),str(up_attention),str(up_archive_count),str(up_like_num),fill(up_sign,width=50)])
print(table)


#获取对应视频BV号下的评论
def get_BV_comment(oid):
url = "https://api.bilibili.com/x/v2/reply/main?callback=&jsonp=jsonp&next=0&type=1&mode=3&plat=1&oid=" + str(oid)
headers = {
'Cookie': '1', #cookie可以随意设置
'User-Agent': 'Mozilla/5.0'
}
resp = requests.get(url, headers=headers)

jsons = json.loads(resp.text)
table = prettytable.PrettyTable()
# 定义表头
table.field_names = ['姓名','评论','评论时间']
table.align['姓名']='l' #左对齐
table.align['评论']='l' #左对齐
for i in jsons['data']['replies']:
#获取评论人的姓名
name = i['member']['uname']
#获取评论人的评论内容
comment = i['content']['message']
#获取评论时间
time = i['ctime']

# 增加一行数据,列表里的元素按照顺序对应表头
table.add_row([fill(name,width=50),fill(comment,width=50),to_time(time)])
print(table)

#将ctime时间戳转换成时间格式
def to_time(time_stamp):
timeArray = datetime.datetime.fromtimestamp(time_stamp)
otherStyleTime = timeArray.strftime("%Y--%m--%d %H:%M:%S")
return otherStyleTime

if __name__=="__main__":
BV_ID = input("请输入视频BV号:")
Input_BV(BV_ID);

效果

此处的评论没有爬完,只爬了最先盖楼的那个人。

Author: jdr
Link: https://jdr2021.github.io/2021/10/24/python3-根据视频BV号爬B站视频的评论消息和UP主的个人信息/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.