import requests from bs4 import BeautifulSoup import jieba.analyse import jieba.posseg as pseg from snownlp import SnowNLP import matplotlib.pyplot as plt # 设置请求头,模拟浏览器访问 headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'} # 获取网页内容 def get_html(url): resp = requests.get(url, headers=headers) resp.encoding = resp.apparent_encoding html = resp.text return html # 获取新闻列表 def get_news_list(url): html = get_html(url) soup = BeautifulSoup(html, 'html.parser') news_list = soup.find_all('a', class_="news_title") return news_list # 对文本进行情感分析 def sentiment_analysis(text): s = SnowNLP(text) return s.sentiments # 对文本进行关键词提取 def keyword_extraction(text): keywords = jieba.analyse.extract_tags(text, topK=10, withWeight=True, allowPOS=('n', 'vn', 'v')) return keywords # 对新闻进行分析 def analyze_news(url): news_list = get_news_list(url) senti_scores = [] # 情感分数列表 keyword_dict = {} # 关键词词频字典 for news in news_list: title = news.get_text().strip() link = news['href'] content = get_html(link) soup = BeautifulSoup(content, 'html.parser') text = soup.find('div', class_='article').get_text().strip() # 计算情感分数 senti_score = sentiment_analysis(text) senti_scores.append(senti_score) # 提取关键词 keywords = keyword_extraction(text) for keyword in keywords: if keyword[0] in keyword_dict: keyword_dict[keyword[0]] += keyword[1] else: keyword_dict[keyword[0]] = keyword[1] # 绘制情感分数直方图 plt.hist(senti_scores, bins=10, color='skyblue') plt.xlabel('Sentiment Score') plt.ylabel('Number of News') plt.title('Sentiment Analysis') plt.show() # 输出关键词词频排名 keyword_list = sorted(keyword_dict.items(), key=lambda x: x[1], reverse=True) print('Top 10 keywords:') for i in range(10): print('{}. {} - {:.2f}'.format(i+1, keyword_list[i][0], keyword_list[i][1])) if __name__ == '__main__': url = 'https://www.sina.com.cn/' analyze_news(url)
时间: 2024-04-25 12:26:26 浏览: 144
这是一段Python代码,用于对新闻进行情感分析和关键词提取。它使用了requests库来获取网页内容,使用BeautifulSoup库来解析HTML文档,使用jieba库来进行中文分词和关键词提取,使用SnowNLP库来进行情感分析,使用matplotlib库来绘制情感分数直方图。在主函数中,它调用了get_news_list()函数来获取新闻列表,然后对每篇新闻进行情感分析和关键词提取,并将情感分数和关键词词频存储到列表和字典中。最后,它绘制情感分数直方图,输出关键词词频排名。
相关问题
解释一下这串代码import requests import xlwt import re from wordcloud import WordCloud import jieba import matplotlib.pyplot as plt
这串代码是Python代码,主要使用了requests、xlwt、re、WordCloud、jieba和matplotlib.pyplot等库,实现了一些文字处理相关的功能,比如从网页中获取数据、将数据存储到Excel表格中、进行正则表达式匹配、生成词云等。
请帮我解释这段代码import requests import xlwt import re from wordcloud import WordCloud import jieba import matplotlib.pyplot as plt
这段代码是Python语言的代码,使用了几个库:requests库,xlwt库,re库,wordcloud库,jieba库和matplotlib库。requests库可以用来向网页发送请求并获取响应,xlwt库可以将数据写入Excel文件,re库提供了正则表达式工具,wordcloud库可以制作词云,jieba库是用于中文分词的库,matplotlib库可以制作各种图表。代码中先导入了这几个库,然后引入相应的功能。
阅读全文
相关推荐
![py](https://img-home.csdnimg.cn/images/20250102104920.png)
![py](https://img-home.csdnimg.cn/images/20250102104920.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)