前言
作为一个后现代网络少年,无意间看到涛姐(刘敏涛)的千万直拍,加上有才的网友,于是顺利承包了所有的笑点,实在是上头,pick!pick!pick!
爬取思路
1. 利用requests请求网页并使用正则获取弹幕
2. 生成专属词云
步骤
1. 在涛姐的视频中,选空白处右击——>检查,得到以下界面:
2. 得到上面的url,复制黏贴打开,可以得到弹幕信息:
代码块
1. 导入相关库:
import requests
import re
import wordcloud
import PIL.Image as image
import numpy as np
2. 获取网页和数据:
def getHtmlText(url):
response = requests.get(url)
response.raise_for_status() # 响应状态码 200
# 有时候设置meta标签查看的编码依旧乱码,此时可以使用apparent_encoding查看真实的编码
response.encoding = response.apparent_encoding
data = response.content.decode('utf-8')
pat = '<d p=".*?">(.*?)</d>'
allComment = re.findall(pat, data, re.S)
# print(allComment)
with open('./弹幕.txt', 'a', encoding='utf-8-sig') as f:
for i in allComment:
f.write(i + '\n')
print("写入成功!")
3. 生成专属词云:
def showPic():
img = np.array(image.open('demo.jpg'))
with open('弹幕.txt', 'r', encoding='utf-8') as f:
text = f.read()
w = wordcloud.WordCloud(font_path = "msyh.ttc", mask=img, scale=15, width=1000, height=700, background_color='white')
w.generate(text)
w.to_file("taojie.png")
print("生成成功!")
4. 调用方法:
url = 'https://api.bilibili.com/x/v1/dm/list.so?oid=187988454'
getHtmlText(url)
showPic()
5. 完整代码:
import requests
import re
import wordcloud
import PIL.Image as image
import numpy as np
def getHtmlText(url):
response = requests.get(url)
response.raise_for_status()
response.encoding = response.apparent_encoding
data = response.content.decode('utf-8')
pat = '<d p=".*?">(.*?)</d>'
all_comment = re.findall(pat, data, re.S)
# print(all_comment)
with open('./弹幕.txt', 'a', encoding='utf-8-sig') as f:
for i in allComment:
f.write(i + '\n')
print("写入成功!")
def showPic():
img = np.array(image.open('demo.jpg'))
with open('弹幕.txt', 'r', encoding='utf-8') as f:
text = f.read()
w = wordcloud.WordCloud(font_path = "msyh.ttc", mask=img, scale=15, width=1000, height=700, background_color='white')
w.generate(text)
w.to_file("taojie.png")
print("生成成功!")
if __name__ == '__main__':
url = 'https://api.bilibili.com/x/v1/dm/list.so?oid=187988454'
getHtmlText(url)
showPic()
特别注意:demo.jpg是我随便找的图,不加你会报错的