标签:join try nbsp inf height 效果 如何 bsp gen
1.首先先安装了request库和bs4库,然后正好最近在网上看到了一篇自己比较感兴趣的文章,就决定爬它了。
2.博文主页:
https://zhidao.baidu.com/daily/view?id=203615
3.代码:
import requests
from bs4 import BeautifulSoup
import re
url="https://zhidao.baidu.com/daily/view?id=203615"
r=requests.get(url)
try:
r.raise_for_status()
r.encoding=r.apparent_encoding
message=r.text
except:
print("ERROR")
soup=BeautifulSoup(message,"html.parser")
songlist=soup.find_all("p")
for i in range(0,39):
print(songlist[i].get_text())
4.最终效果:
5.创建一个txt文件然后将结果复制粘贴进去,然后弄词云。
词云代码:import jieba
import wordcloud
f=open("爬虫.txt",encoding="utf-8")
t=f.read()
f.close()
words=jieba.lcut(t)
txt=" ".join(words)
w=wordcloud.WordCloud(font_path="msyh.ttc",width=1000,\
height=1000,background_color="white",\
stopwords={"我们"},max_words=100)
w.generate(txt)
w.to_file("爬虫.png")
词云效果:
标签:join try nbsp inf height 效果 如何 bsp gen
原文地址:https://www.cnblogs.com/qq9962/p/12818497.html