由于原始代码已经提供了一个很好的实例,以下是核心函数的简化版本,展示如何爬取城市评论并进行情感分析:
import requests
from bs4 import BeautifulSoup
from textblob import TextBlob
# 爬取评论并进行情感分析的函数
def crawl_and_analyze_comments(url):
# 发送HTTP请求
response = requests.get(url)
# 解析网页
soup = BeautifulSoup(response.text, 'html.parser')
# 提取评论
comments = soup.find_all('p', class_='comment-content')
# 初始化情感分析计数器
positive_count, negative_count, neutral_count = 0, 0, 0
for comment in comments:
text = comment.get_text()
# 对评论进行情感分析
analysis = TextBlob(text)
sentiment = analysis.sentiment.polarity
if sentiment > 0:
positive_count += 1
elif sentiment < 0:
negative_count += 1
else:
neutral_count += 1
# 计算情感比例
positive_ratio = positive_count / (positive_count + negative_count + neutral_count)
negative_ratio = negative_count / (positive_count + negative_count + neutral_count)
return positive_ratio, negative_ratio
# 示例URL
example_url = 'https://www.tripadvisor.cn/Attraction_Review-g186338-d10351889-Reviews-Xian_Tian_An_Men_Tian_An_Men_Guan-Chengzhou_Sichuan_Province.html'
# 执行情感分析
positive_ratio, negative_ratio = crawl_and_analyze_comments(example_url)
print(f"Positive Ratio: {positive_ratio:.2f}, Negative Ratio: {negative_ratio:.2f}")
这段代码展示了如何使用requests库获取网页内容,使用BeautifulSoup进行网页解析,以及如何使用TextBlob进行情感分析。代码简洁,注重逻辑性,可以作为爬虫和情感分析相关开发的入门示例。