Python爬京东多个商品评论
时间: 2023-08-07 13:24:11 浏览: 76
要爬取京东多个商品的评论,可以使用Python的requests和BeautifulSoup库。以下是一个示例代码,可以爬取指定商品ID的评论:
```python
import requests
from bs4 import BeautifulSoup
# 商品ID列表
product_ids = ['100008348542']
# 评论页URL模板
url_template = 'https://sclub.jd.com/comment/productPageComments.action?callback=fetchJSON_comment98vv{0}&productId={1}&score=0&sortType=5&page={2}&pageSize=10'
# 遍历商品ID列表
for product_id in product_ids:
page = 0
has_next_page = True
while has_next_page:
# 构建评论页URL
url = url_template.format(page, product_id, page + 1)
# 请求评论页
response = requests.get(url)
# 解析JSON数据
json_data = response.text.lstrip('fetchJSON_comment98vv' + str(page) + '(').rstrip(');')
data = json.loads(json_data)
# 获取评论
comments = data['comments']
for comment in comments:
print(comment['content'])
# 检查是否有下一页
has_next_page = data['comments'] != []
page += 1
```
注意:该代码仅供学习参考,爬取评论需要遵守相关法律法规和京东网站的规定。