赞
踩
以下是爬取淘宝商品评论的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)
- # 手动设置User-Agent,避免被网站反爬
- 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)
- # 使用正则表达式获取json数据
- pattern = re.compile(r"(\{.*\})")
- match = pattern.search(response.text)
- if match:
- json_data = match.group(1)
- # 解析json数据
- 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) # 将商品ID传入函数即可获取评论
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。