《灵笼》艺画开天生产,B站独播,国漫里面制作算是精良,但是剧情有太多瑕疵。评论两极分化,好的说非常好,差的说非常差。看看弹幕说的啥吧
思路
用到的库
代码思路来自B站python学习者
# 爬取数据
import csv
# 数据请求库
import requests
# 正则表达式
import re
# 分词
import jieba
# 词云
import wordcloud
# 1.定位url
url='https://api.bilibili.com/x/v2/dm/history?type=1&oid=129528808&date=2020-08-28'
# 2.模拟登陆
# 设置h请求头防止反扒拦截
# 因为b站是登录后查看弹幕,因此这里需要加入自己电脑的cookie
headers={
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:80.0) Gecko/20100101 Firefox/80.0',
"Cookie":"_uuid=445F64D3-1530-41CF-09EC-C6029EF29EA659147infoc; buvid3=54281361-1A51-46A7-838B-7FB1214C81B953936infoc; CURRENT_FNVAL=16; LIVE_BUVID=AUTO9915845422486102; rpdid=|(u)~mYY~u0J'ul)RlRkkR); sid=4y1wx1oi; DedeUserID=229593267; DedeUserID__ckMd5=72ee797eb51fb8c3; SESSDATA=b7620543%2C1600240037%2Cd737d*31; bili_jct=03269466eb702a213723a0585db59cbe; bp_t_offset_229593267=428995725967649604; CURRENT_QUALITY=80; PVID=1; _ga=GA1.2.1605929815.1586006097; bp_video_offset_229593267=428995725967649604; blackside_state=1; bfe_id=fdfaf33a01b88dd4692ca80f00c2de7f"
}
# 请求数据
resp = requests.get(url,headers=headers)
# 解码数据,防止乱码
html=resp.content.decode('utf-8')
# 3.解析网页提取字幕
# 通过正则表达式,对返回html中的弹幕提取
res=re.compile('<d.*?>(.*?)</d>')
danmu=re.findall(res,html)
# 4.保存数据
for i in danmu:
with open (r'D:\360MoveData\Users\cmusunqi\Documents\GitHub\R_and_python\python\词云和爬虫\弹幕.csv','a',newline='',encoding='utf-8') as f:
writer=csv.writer(f)
danmu=[]
danmu.append(i)
writer.writerow(danmu)
# 词云绘制============================================================
# 读取保存的csv文件
f = open (r'D:\360MoveData\Users\cmusunqi\Documents\GitHub\R_and_python\python\词云和爬虫\弹幕.csv',encoding='utf-8')
txt=f.read()
# jieba分词
txt_list=jieba.lcut(txt)
# 将分词后的列表通过空格连接
string=' '.join(txt_list)
# 绘制词云,内置参数需查看官方代码
w=wordcloud.WordCloud(
width=1000,
height=700,
background_color='white',
font_path="msyh.ttc",
scale=15,
stopwords={" "},
contour_width=5,
contour_color='red'
)
# 导出图片为png
w.generate(string)
w.to_file(r'D:\360MoveData\Users\cmusunqi\Documents\GitHub\R_and_python\python\词云和爬虫\ciyun.png')
绘制的词云最大的字和是害怕和猝不及防,你害怕还看个P呦。
love&peace