番名:我的青春恋爱物语果然有问题3 PV
效果图:
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/f72b9a86a54fab625c31503de74be55d.png)
源代码:
import wordcloud
import requests
import re
import csv
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36"}
url = 'https://api.bilibili.com/x/v1/dm/list.so?oid=170310173'
response = requests.get(url=url,headers=headers)
html_doc = response.content.decode('utf-8')
format = re.compile("<d.*?>(.*?)</d>")
DanMu = format.findall(html_doc)
for i in DanMu:
with open(r'D:\pytest\csv.csv',"a", newline='',encoding='utf-8-sig') as csvfile:
writer= csv.writer(csvfile)
danmu = []
danmu.append(i)
writer.writerow(danmu)
f = open('D:\pytest\csv.csv',encoding='utf-8')
txt = f.read()
w = wordcloud.WordCloud(width=1000, height=700, background_color='white', font_path='msyh.ttc')
w.generate(txt)
w.to_file('D:\pytest\output.png')