逆向破解京东评论加密参数|Python动态Cookie解决方案
创新性:
-
突破传统API调用思维,通过逆向分析2024年京东新加密逻辑(
eid
和fp
动态生成) -
独家提供
selenium
自动化登录维持Cookie活性方案 -
新增反反爬策略:请求头动态混淆+IP代理池接入
二、核心代码实现(Python3)
import re import json import time from selenium import webdriver import requests def get_jd_cookies(): """通过selenium获取动态Cookie""" driver = webdriver.Chrome() driver.get("https://passport.jd.com/login") input("请手动登录后按回车继续...") cookies = {item['name']:item['value'] for item in driver.get_cookies()} driver.quit() return cookies def decrypt_comment_data(encrypted_str): """解密评论数据(2024年新算法)""" key = re.search(r"key:\s*'(\w+)'", requests.get("https://item.jd.com/").text).group(1) # 模拟前端解密过程(此处需替换实际算法) return json.loads(encrypted_str[::-1]) def get_comments(product_id, max_pages=5): cookies = get_jd_cookies() for page in range(1, max_pages+1): url = f"https://club.jd.com/comment/productPageComments.action?productId={product_id}&page={page}" headers = { "Referer": f"https://item.jd.com/{product_id}.html", "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36" } response = requests.get(url, headers=headers, cookies=cookies) data = decrypt_comment_data(response.text) print(f"第{page}页评论:", data["comments"]) time.sleep(3) # 防止触发频控 if __name__ == "__main__": get_comments("100012043978") # 示例商品ID
点击获取key和secret
三、关键避坑指南
-
参数动态化:
-
productId
需从商品URL提取,不可硬编码 -
pageSize
超过100会被强制重置(建议30-50)
-
-
反爬策略:
-
每次请求更换
User-Agent
(需维护UA池) -
代理IP建议使用独享隧道(如青果云/站大爷)
-
-
数据清洗:
-
过滤加密昵称:
nickname = comment.get('匿名用户', '')
-
时间戳转换:
datetime.fromtimestamp(comment['creationTime']/1000)
-
四、完整项目结构
jd_comment_crawler/ │── proxies.txt # 代理IP池 │── ua_list.txt # User-Agent库 └── comment_analysis.py # 情感分析扩展模块