1. 程式人生 > >爬蟲大作業-爬區a9vg電玩部落ps4專區

爬蟲大作業-爬區a9vg電玩部落ps4專區

IT () als lec href news app 分析 word

1.選一個自己感興趣的主題或網站。(所有同學不能雷同)

2.用python 編寫爬蟲程序,從網絡上爬取相關主題的數據。

def writeNewsDetail(content):
    f = open(a9vg.txt,a,encoding=utf-8)
    f.write(content)
    f.close()

def getNewsDetail(url):
    res2 = requests.get(url)
    res2.encoding = utf-8
    soup2 = BeautifulSoup(res2.text, html.parser
) news = {} news[content] = soup2.select(.art-ctn)[0].text # 爬取ps4專區新聞的正文 writeNewsDetail(news[content]) news[newsurl]=url return(news) def getListPage(pageUrl): res = requests.get(pageUrl) res.encoding = utf-8 soup = BeautifulSoup(res.text,html.parser) newsList
=[] for news in soup.select(.tab-ctn dl): if len(news.select(h3)) > 0: a = news.select(a)[0].attrs[href] print(a) newsList.append(getNewsDetail(a)) return(newsList)

3.對爬了的數據進行文本分析,生成詞雲。

def cutword():
    text=‘‘
    f = open(a9vg.txt, 
r, encoding=utf8) lines = f.readlines() for line in lines: text += line for key in analyse.extract_tags(text, 50, withWeight=False): # 使用jieba.analyse.extract_tags()參數提取關鍵字,默認參數為50 print(key) jieba.add_word(奧丁) words_ls = jieba.cut(text) words_split = " ".join(words_ls) print(words_ls) return words_split def wordspic(): wordsp=cutword() Stopwords = [programs,view,tudou,www,http,com,https,qq,page,殺死,渡鴉] wc = WordCloud() # 字體這裏有個坑,一定要設這個參數。否則會顯示一堆小方框 wc.stopwords=Stopwords wc.max_words=200 wc.background_color=white wc.font_path="simhei.ttf" # 黑體 my_wordcloud = wc.generate(wordsp) plt.imshow(my_wordcloud) plt.axis("off") plt.show() wc.to_file(ttt.png) # 保存圖片文件

4.對文本分析結果進行解釋說明。

通過使用第三方的jieba庫進行中文分詞,其中有過多新聞正文內容包含視頻鏈接,所以通過設計了停用詞,去掉一些詞

關鍵詞如下

技術分享圖片

5.寫一篇完整的博客,描述上述實現過程、遇到的問題及解決辦法、數據分析思想及結論。

在進行下載安裝第三方的庫的時候,wordcloud下載失敗,查了各種問題最後通過https://www.lfd.uci.edu/~gohlke/pythonlibs/這個網站下載對應py版本對應系統位數的庫進行安裝。

6.最後提交爬取的全部數據、爬蟲及數據分析源代碼。

爬取的鏈接,內容如下(使用了pandas這個庫進行輸出到控制臺)

技術分享圖片

詞雲如下:

技術分享圖片

爬蟲大作業-爬區a9vg電玩部落ps4專區