爬取B站弹幕并且制作词云
爬取弹幕
1. 从手机端口进入网页爬取找到接口
2.代码
import requests
from lxml import etree
import numpy as np
url='https://api.bilibili.com/x/v1/dm/list.so?oid=198835779'
headers= {
'User-Agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Mobile Safari/537.36',
}
response = requests.get(url,headers= headers)
# response.encoding='utf-8'
# print(response.text)
# print(response.content)
print(type(response.text))
html=etree.HTML(response.content)
t=etree.tostring(html)
# print(t)
words = html.xpath('/html//d/text()')
with open('word.txt','w')as f:
for word in words:
f.write(word+' ')
print(words)
利用Xpath获取弹幕信息
然后输入到文件当中
每一次的都是不同的弹幕所以不用担心弹幕数量过少
制作词云
利用的是wordscloud库
1.文件读取
2.代码
#coding:utf-8
from matplotlib import pyplot as plt
from wordcloud import WordCloud
import jieba
f=open(r'word.txt','r',encoding='gbk').read()
font = r'C:\Windows\Fonts\FZSTK.TTF'
cloud=WordCloud(
font_path=font,
width=1000,
height=800,
margin=2
).generate(f)
plt.imshow(cloud)
plt.axis('off')
plt.show()
需要有中文字体,这个只需要在自己的系统文件找就可以
参数 有:宽,长,边框厚度,利用到了matplotlib中的imshow和show
结果如下: