1.选一个自己感兴趣的主题或网站。(所有同学不能雷同)
2.用python 编写爬虫程序,从网络上爬取相关主题的数据。
3.对爬了的数据进行文本分析,生成词云。
4.对文本分析结果进行解释说明。
5.写一篇完整的博客,描述上述实现过程、遇到的问题及解决办法、数据分析思想及结论。
6.最后提交爬取的全部数据、爬虫及数据分析源代码。
import urllib.request as ur from urllib.request import Request from bs4 import BeautifulSoup as bs import re def gethtml(url): header = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36'} req = Request(url, headers=header) html = ur.urlopen(req).read().decode('utf-8') return html def getinfo(html): soup = bs(html,'html.parser') info = soup.select('.detail_left_cnt #infodata p') # for i in range(3): # info[i] = info[i].text # print(info) return info if __name__=="__main__": url = "https://kandian.youth.cn/index/detail?sign=ev0EmQzwYx4qZjo" html1 = gethtml(url) info = getinfo(html1) for i in info: f = open('info2.txt', 'a+', encoding='utf-8') f.write(re.sub('s+', '', i.get_text())) f.close() import jieba from wordcloud import WordCloud import matplotlib.pyplot as d info = open('info2.txt','r',encoding='utf-8').read() text = '' text += ' '.join(jieba.lcut(info)) window = WordCloud(font_path='C:WindowsFontsSTZHONGS.TTF',background_color='White',max_words=50) window.generate_from_text(text) d.imshow(window) # p.imshow(wc.recolor(color_func=00ff00)) d.axis("off") d.show() window.to_file('hdz.jpg')