爬取人民网主页信息并以词云图展示其中新闻热词的Python代码。
时间: 2025-01-19 10:14:04 浏览: 61
爬取人民网主页信息并制作词云图通常需要使用Python的网络请求库(如requests)、HTML解析库(如BeautifulSoup)以及词云生成库(如wordcloud)。以下是一个简单的示例代码:
```python
import requests
from bs4 import BeautifulSoup
from wordcloud import WordCloud
import matplotlib.pyplot as plt
# 请求人民网首页
url = "http://www.people.com.cn/"
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
# 提取文章标题作为文本源
text = ''
for article in soup.find_all('a', class_='title'):
text += article.text + ' '
# 清理文本,去除特殊字符和停用词(这里假设已经有一个停用词列表)
stopwords = [] # 你需要一个实际的停用词列表
filtered_text = [word for word in text.split() if word not in stopwords and '\n' not in word]
# 创建词云
wordcloud = WordCloud(font_path='simhei.ttf', background_color='white', width=800, height=600).generate(filtered_text)
# 显示词云
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis('off')
plt.show()
```
注意:
1. 这个例子假设网页结构允许我们直接提取出标题元素,实际情况下可能需要更复杂的HTML解析。
2. `font_path`参数需要你有合适的字体文件,以便于中文显示。
3. 对于大规模的抓取,可能需要处理反爬虫策略和频率限制。
阅读全文
相关推荐
















