1. 前言
本文记录如何使用 Python 爬取携程景区的评论数据,并进行简单的分析与存储。
2. 爬虫实现
2.1 定位接口
- 打开携程网,找到某个景区点击跳转到详情页面。

- 按 F12 打开开发者工具,然后点击下评论下一页来监听是否有网络请求更新。如果没有那就看页面地址栏是否发生变化,前者是动态更新(post 请求,通过调后端接口完成数据更新),后者是静态更新(get 请求,通过 html 页面更新数据)。
通过搜索评论内容定位到评论数据是通过调 getCommentCollapseList 接口返回的。

- 那么我们现在已经知道数据在哪个接口中,接下来就需要在本地模拟调用这个请求即可,这里我是用 Python 的 requests 库实现。你问我怎么知道调这个请求需要携带这些参数?
那我告诉你一个快速又便捷的方法就是右键复制这个请求的 curl(bash) 去拿到 https://curlconverter.com/ 网站粘贴,就会自动输出完整的调用请求代码。
import requests
cookies = {
'GUID': '09031069217559688465',
'MKT_CKID': '1751274744072.9fx30.ncpi',
'_RSG': 'Ce4EW5dni37P3spnPcTGtA',
# ... (其他 Cookie 字段)
}
headers = {
'accept': '*/*',
'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
'cache-control': 'no-cache',
'content-type': 'application/json',
'cookieorigin': 'https://you.ctrip.com',
'origin': 'https://you.ctrip.com',
'pragma': 'no-cache',
: ,
: ,
: ,
: ,
: ,
: ,
: ,
: ,
: ,
: ,
: ,
: ,
: ,
: ,
: ,
}
params = {
: ,
: ,
}
json_data = {
: {
: ,
: ,
: ,
: ,
: ,
: ,
: ,
: ,
: ,
},
: {
: ,
: ,
: ,
: ,
: ,
: ,
: ,
: ,
: [],
},
}
response = requests.post(
,
params=params,
cookies=cookies,
headers=headers,
json=json_data,
)
(response.json())






