1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
| import requests from lxml import etree import datetime import random as r from datetime import timedelta import jieba from wordcloud import WordCloud from PIL import Image import numpy as np import matplotlib.pyplot as plt
useragent = [ 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/18.17763', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.75 Safari/537.36', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:65.0) Gecko/20100101 Firefox/65.0', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.0.3 Safari/605.1.15', 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36'
]
headers={ 'user-agent': r.choice(useragent), "cookie":"_uuid=72919017-AD53-374F-B5D7-9B83AB97EED158304infoc; buvid3=36B30709-3B4E-4FF4-AAC0-4882356AF274143084infoc; sid=aifpqhjy; DedeUserID=626120309; DedeUserID__ckMd5=cbd6baccd20c90d4; SESSDATA=d8ed0e66%2C1615632718%2Cd3403*91; bili_jct=fdb9c69e8b41f6ee814b34d385be5e20; CURRENT_FNVAL=80; blackside_state=1; rpdid=|(u|JRRkuJu)0J'uY|||)|~R~; LIVE_BUVID=AUTO6816006815353517; PVID=3; CURRENT_QUALITY=80; bfe_id=1bad38f44e358ca77469025e0405c4a6; bp_video_offset_626120309=438479623642879626; bp_t_offset_626120309=438479623642879626" } def getdanmu(date): date = datetime.datetime.strptime(date, "%Y-%m-%d") + timedelta(days=-1) print(date.strftime("%Y-%m-%d")) res=requests.get(f'https://api.bilibili.com/x/v2/dm/history?type=1&oid=238498824&date={date.strftime("%Y-%m-%d")}',headers=headers) xml=etree.HTML(res.text.encode(res.encoding)) text=xml.xpath('//d//text()') for i in text: with open("bilibili.txt","a+") as f: f.write(i) f.write("\n") if text: getdanmu(date.strftime("%Y-%m-%d"))
def cutTest(): with open("bilibili.txt","r") as f: for i in f.readlines(): for j in jieba.lcut(i): with open("cut.txt","a+") as w: with open('stop.txt',"r") as s: if w.readline()!=j: w.write(j) w.write('\n')
def word(): text = open('cut.txt').read()
mask = np.array(Image.open("beijing.jpeg")) wc = WordCloud(mask=mask, font_path='FZQiuDYRHJW.TTF', mode='RGBA', background_color=None,collocations=False).generate(text)
plt.imshow(wc, interpolation='bilinear') plt.axis("off") plt.show()
wc.to_file('wordcloud4.png')
getdanmu("2020-09-25") cutTest() word()
|