-
Notifications
You must be signed in to change notification settings - Fork 0
/
bilibili-comment.py
55 lines (47 loc) · 1.76 KB
/
bilibili-comment.py
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
import re
import time
import requests
import numpy as np
import pandas as pd
from PIL import Image
from matplotlib import pyplot as plt
from wordcloud import WordCloud
def clean(comment_list):
comment = []
for i in comment_list:
if i not in ['0','1','2','3','4','5','6','7','8','9',0,1,2,3,4,5,6,7,8,9]:
comment.append(i)
df = pd.DataFrame()
df['弹幕'] = comment
df.to_csv('comment.csv')
return ' '.join(comment)
# def clean(comment_list):
# for i in comment_list:
# if i in ['0','1','2','3','4','5','6','7','8','9',0,1,2,3,4,5,6,7,8,9]:
# comment_list.remove(i)
# return ' '.join(comment_list)
def page(url, headers):
response = requests.get(url, headers=headers)
response.encoding = 'utf8'
if response.status_code == 200:
return response.text
def analysis(comment):
comment_list = re.findall(r'<d p=.*?>(.*?)</d>', comment)
comment_str = clean(comment_list)
mask = np.array(Image.open('ChinaMap.png'))
wc = WordCloud(background_color=None, repeat=True, height=480, width=854,mask=mask, mode='RGBA', font_path='千图纤墨体.ttf')
wc.generate(comment_str)
plt.axis("off")
plt.imshow(wc, interpolation="bilinear")
plt.show()
wc.to_file('c.png')
if __name__ == '__main__':
url = 'https://www.bilibili.com/video/BV12t4y1n7b6?spm_id_from=333.337.search-card.all.click&vd_source=655ba79c7a5400218ba336bf2746db0e'
headers = {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36'
}
html = page(url, headers)
cid = re.findall(r'"cid":(.*?),', html)[0]
comment_url = f'https://comment.bilibili.com/{cid}.xml'
comment = page(comment_url, headers)
analysis(comment)