网站制作模板教案,全国购物网站排名,关于集团网站建设的修改请示,传奇手游发布网站一、评论爬取
以百度贴吧中“美团骑手吧”为例#xff0c;对页面中的帖子评论进行爬取#xff0c;并将结果以json的格式保存到本地中。
from lxml import etree
import requests
import json# 根据网页url获取评论
def GetComments(url):# 使用requests库发送GET请求#…一、评论爬取
以百度贴吧中“美团骑手吧”为例对页面中的帖子评论进行爬取并将结果以json的格式保存到本地中。
from lxml import etree
import requests
import json# 根据网页url获取评论
def GetComments(url):# 使用requests库发送GET请求获取网页内容并将响应内容编码设置为utf-8response requests.get(url)response.encoding utf-8content response.text #将响应内容转换为字符串ret etree.HTML(content) #使用lxml库的HTML方法解析网页内容# 使用XPath表达式提取所有帖子信息lists ret.xpath(//li[class j_thread_list clearfix thread_item_box])# 依次获取每条帖子的评论信息并存放在my_list列表当中for list in lists:global my_listcomment str(list.xpath(.//div[classthreadlist_abs threadlist_abs_onlyline ]/text())[0].strip())my_list.append(comment)if __name__ __main__:base_url https://tieba.baidu.com/f?kw%E7%BE%8E%E5%9B%A2%E9%AA%91%E6%89%8Bieutf-8pnmy_list []# 每50条数据为1页依次获取多个页面的帖子评论for i in range(0,50,50):url base_url f{i}GetComments(url)# 将获取的帖子评论以json格式进行保存with open(list_file.json, w, encodingutf-8) as file:json.dump(my_list, file, ensure_asciiFalse, indent4)二、情感分析
根据上述爬取到的评论利用snowlp工具进行情感分析将评论分为“积极”“中性”消极“并根据数量占比进行可视化。
from snownlp import SnowNLP
import matplotlib.pyplot as plt
import json# 定义全局变量comments,用于存储评论数据
global comments# 获取在本地存放的评论数据
with open(list_file.json, r, encodingutf-8) as file:comments json.load(file)# 利用情感分析函数对每一条评论进行分析判断其情感色彩
def sentiment_analysis(text):s SnowNLP(text)if s.sentiments 0.6:return 积极elif s.sentiments 0.4:return 消极else:return 中性# 对评论数据进行情感分析
sentiments [sentiment_analysis(comment) for comment in comments]# 统计各类情感的数量
positive_count sentiments.count(积极)
negative_count sentiments.count(消极)
neutral_count sentiments.count(中性)# 可视化处理
labels [积极, 消极, 中性]
sizes [positive_count, negative_count, neutral_count]
colors [green, red, gray]
explode (0.1, 0, 0)plt.rcParams[font.sans-serif] [SimHei] # 设置字体为黑体plt.pie(sizes, explodeexplode, labelslabels, colorscolors, autopct%1.1f%%, shadowTrue, startangle90)
plt.axis(equal)
plt.show()