参考了许多大佬的代码+自己慢慢调,虽然还是不完全理解,但突然对python有了点兴趣,有的库可太好用了 https://blog.csdn.net/viafcccy/article/details/85217934
https://blog.csdn.net/qq_30638831/article/details/79959133
代码
import re
import requests
import jieba
from scipy.misc import imread
import matplotlib.pyplot as plt
from bs4 import BeautifulSoup
from wordcloud import WordCloud
def getHTML(url):
try:
r = requests.get(url, timeout=30)
r.raise_for_status()
r.encoding = r.apparent_encoding
return r.text
except:
return ""
def getContent(url):
html = getHTML(url)
soup = BeautifulSoup(html, 'html.parser')
title=soup.select('.pages_content')
paras_tmp = soup.select('p')
paras = paras_tmp[1:2]+paras_tmp[4:]
return paras
def saveFile(text):
f = open("D:\incase\大三上\Python程序设计\作业\李克强report.txt", 'w+')
for t in text:
if len(t) > 0:
f.writelines(t.get_text() + "\n\n")
f.close()
url = 'http://www.gov.cn/premier/2021-03/12/content_5592671.htm'
text = getContent(url)
saveFile(text)
bg_pic = imread('D:\incase\大三上\Python程序设计\作业\素材\ground.jpg')
txt=open("D:\incase\大三上\Python程序设计\作业\李克强report.txt", "r").read()
words=" ".join(jieba.cut(txt))
wordcloud = WordCloud(font_path='msyhbd.ttc',mask=bg_pic,background_color='red',stopwords='的 和',max_font_size=100,max_words=300,width=600,height=800).generate(words)
plt.figure(figsize=((10, 8)))
plt.imshow(wordcloud)
plt.axis("off")
plt.show()
wordcloud.to_file('D:\incase\大三上\Python程序设计\作业\李克强report_picture.png')
运行结果: 题外话 最近超级焦虑呢,去看了一下今年的校招,感觉软件工程师如果要后端,基本都是java,而其他项目方面的经验更是一点都没,每天觉得自己菜的不行…c语言现在作为高级语言基础,更多作为嵌入式底层开发,相较之下,python的应用选择面更广,看来今年如果pat乙级c能过的话,寒假一方面准备蓝桥杯,一方面准备考研…我真的是热爱自讨苦吃,从文转理,现在又想朝着最卷的CS冲刺,算了,尽力就好
|