1.選一個自己感興趣的主題或網站。(所有同學不能雷同)
https://www.bilibili.com/video/av22224421
2.用python 編寫爬蟲程序,從網絡上爬取相關主題的數據。
3.對爬了的數據進行文本分析,生成詞雲。
import requests import jieba import pandas import matplotlib.pyplot as plt from wordcloud import WordCloud ,ImageColorGenerator from bs4 import BeautifulSoup def jieba_cut(sentence): seg = jieba.cut(sentence) segList = [] for i in seg: segList.append((i)) return segList if __name__=='__main__': str='' url='http://comment.bilibili.com/36773399.xml' page=requests.get(url) page.encoding='utf-8' soup=BeautifulSoup(page.text,"html.parser") content=soup.find_all('d') for i in content: str=str+i.text with open('bilibili.txt','w',encoding='utf-8') as f: f.write(str) dict={} with open ('bilibili.txt','r',encoding='utf-8') as f: words=jieba_cut(f.read()) wordslist=set(words) for word in wordslist: dict[word]=words.count(word) mask = plt.imread(r'H:\129\wallhaven-627476.jpg') text=' '.join(words) wc = WordCloud( width=1000, height=800, margin=2, background_color='white', # 設置背景顏色 font_path='C:\Windows\Fonts\STZHONGS.TTF', # 若是有中文的話,這句代碼必須添加,不然會出現方框,不出現漢字 max_words=1000, # 設置最大現實的字數 max_font_size=400, # 設置字體最大值 random_state=50, # 設置有多少種隨機生成狀態,即有多少種配色方案 mask=mask, ) mycloud = wc.generate(text) image_colors = ImageColorGenerator(mask) wc.recolor(color_func=image_colors) wc.to_file('cloudword.jpg')
4.對文本分析結果進行解釋說明。
5.寫一篇完整的博客,描述上述實現過程、遇到的問題及解決辦法、數據分析思想及結論。
找到視頻網站,查找網頁源碼,找出cid,打開彈幕文件XML,開始爬取彈幕存入文本中。在詞頻統計時出現一點小問題,用字典統計。
6.最后提交爬取的全部數據、爬蟲及數據分析源代碼。