Python爬取苏宁易购评论代码
时间: 2023-11-05 22:59:00 浏览: 96
好的,以下是Python爬取苏宁易购评论的代码:
```python
import requests
import json
def get_comments(product_id):
url = 'https://review.suning.com/ajax/cluster_review_lists/general-{}-all-0-total.htm?callback=reviewList'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:86.0) Gecko/20100101 Firefox/86.0'
}
comments = []
page_num = 1
while True:
try:
response = requests.get(url.format(product_id, page_num), headers=headers, timeout=10)
except:
continue
if response.status_code != 200:
break
data = json.loads(response.text.replace('reviewList(', '')[:-2])
if not data['commodityReviews']:
break
for comment in data['commodityReviews']:
comments.append(comment['content'])
page_num += 1
return comments
```
请问还有什么问题吗?我可以为您提供更多帮助。
阅读全文