python爬虫获取携程旅游景点评分和评论
时间: 2024-03-15 17:38:34 浏览: 103
基于python实现爬取携程景点数据与评论数据源码+项目说明.zip
5星 · 资源好评率100%
根据您提供的引用内容,以下是使用Python爬虫获取携程旅游景点评分和评论的示例代码:
```python
import requests
from bs4 import BeautifulSoup
def get_ctrip_reviews(url):
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
}
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.text, 'html.parser')
reviews = []
# 获取评分
ratings = soup.find_all('span', class_='score')
for rating in ratings:
reviews.append({
'rating': rating.text.strip()
})
# 获取评论
comments = soup.find_all('div', class_='comment_single')
for comment in comments:
reviews.append({
'comment': comment.text.strip()
})
return reviews
url = 'https://you.ctrip.com/sight/guizhou147/100.html' # 携程旅游景点的URL
reviews = get_ctrip_reviews(url)
for review in reviews:
if 'rating' in review:
print('评分:', review['rating'])
elif 'comment' in review:
print('评论:', review['comment'])
```
阅读全文