爬虫大作业
# -*- coding: utf-8 -*- import requests from bs4 import BeautifulSoup as bs def getreq(url): header = {'user-agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36'} html = requests.get(url,headers=header).content.decode('utf-8') return html def gettext(html): soup = bs(html,'html.parser') info = soup.select('.post_item_summary') return info if __name__=='__main__': url = "https://www.cnblogs.com/" html = getreq(url) info = gettext(html) for i in info: f = open('i.txt', 'a+', encoding='utf-8') f.write(i.get_text()) f.close()
import jieba import PIL from wordcloud import WordCloud import matplotlib.pyplot as p import os info = open('i.txt','r',encoding='utf-8').read() text = '' text += ' '.join(jieba.lcut(info)) wc = WordCloud(font_path='C:\Windows\Fonts\STZHONGS.TTF',background_color='White',max_words=50) wc.generate_from_text(text) p.imshow(wc) # p.imshow(wc.recolor(color_func=00ff00)) p.axis("off") p.show() # wc.to_file('dream.png')