python爬取淘宝评论代码
时间: 2023-10-27 21:05:50 浏览: 147
Python淘宝评论爬取
以下是Python爬取淘宝商品评论的代码,可以获取指定商品的评价内容:
import requests
import re
import json
def get_comments(item_id):
url = 'https://rate.tmall.com/list_detail_rate.htm?itemId={}¤tPage=1'.format(item_id)
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'}
response = requests.get(url, headers=headers)
pattern = re.compile(r"(\{.*\})")
match = pattern.search(response.text)
if match:
json_data = match.group(1)
data = json.loads(json_data)
comments = data['rateDetail']['rateList']
for comment in comments:
print(comment['content'])
if __name__ == '__main__':
item_id = '600938870110'
get_comments(item_id)
请注意,为了避免被网站反爬,代码中设置了User-Agent头部信息。同时,建议不要抓取太多评论,以免对服务器造成压力。
阅读全文