目录
SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。
爬取弹幕
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)
每一次的都是不同的弹幕所以不用担心弹幕数量过少
制作词云
利用的是wordcloud库
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()
扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄
×
选择打赏方式:
微信
QQ钱包
支付宝
打赏
打赏
打赏
多少都是心意!谢谢大家!!!
×
选择分享方式:
微信扫一扫,分享朋友圈
Or
手机扫一扫,精彩随身带