拼多多Anti_content反扒破解
像我们会做一些汇总需要抓取订单数据,评价,交易数据,和商品推广数据,为了更快更方便的去获取数据我们可以通过接口去获取,也可以避免一些css反扒等等
在我们通过抓包我们能发现每次请求都有携带Anti_content这个参数,但是这个参数又是随机的并且时效很短
转换成代码如果去除了Anti_content这个参数返回的就是错误参数
我分享一个很笨的方法通过调试断点找到生成Anti_content的js代码,把生成Anti_content的js代码扒下来补齐环境执行即可
需要补齐的环境只有Node.js 12.18.1
补齐完成之后通过Python调用执行js即可,后续发送请求携带参数即可
Python代码:
import json
import execjs
import requests
from datetime import datetime, timedelta, timezone
# 常量定义
PDD_URL = "你的URL"
HEADERS_TEMPLATE = {
"你的请求头"
}
def get_now_time():
"""
获取当前时间并格式化为 "yyyy-mm-dd HH:mm:ss" 格式
"""
now = datetime.now()
return now.strftime('%Y-%m-%d %H:%M:%S')
def get_utc_time_range(days=90):
"""
获取当前 UTC 时间戳及指定天数前的 UTC 时间戳
"""
current_time_utc = int(datetime.now(timezone.utc).timestamp())
past_time_utc = int((datetime.now(timezone.utc) - timedelta(days=days)).timestamp())
return past_time_utc, current_time_utc
def generate_anti_content(js_path):
"""
调用外部 JavaScript 代码生成 anti-content 字段
"""
with open(js_path, encoding='utf-8') as js_file:
js_code = js_file.read()
ctx = execjs.compile(js_code)
return ctx.call('antigain')
def prepare_cookies(arr_cookies):
"""
将 Cookie 列表格式化为字符串
"""
return "; ".join([f"{cookie['name']}={cookie['value']}" for cookie in arr_cookies])
def request_sh(arr_cookies, js_path, goods_id):
"""
向拼多多 API 发起请求,获取指定商品的备注信息
"""
# 生成 anti-content
anti_content = generate_anti_content(js_path)
# 准备 Cookie 和时间范围
cookies_str = prepare_cookies(arr_cookies)
start_time, end_time = get_utc_time_range()
# 设置请求体
payload = json.dumps({
"orderSn": goods_id,
"orderType": 0,
"afterSaleType": 0,
"remarkStatus": -1,
"urgeShippingStatus": -1,
"groupStartTime": start_time,
"groupEndTime": end_time,
"pageNumber": 1,
"pageSize": 20,
"sortType": 7,
"hideRegionBlackDelayShipping": False,
"mobile": ""
})
# 设置请求头
headers = HEADERS_TEMPLATE.copy()
headers["anti-content"] = anti_content
headers["cookie"] = cookies_str
# 发起请求并处理响应
for attempt in range(1, 4): # 重试 3 次
try:
response = requests.post(PDD_URL, headers=headers, data=payload)
response.raise_for_status() # 检查 HTTP 响应状态码
result = response.json()
return result["result"]["pageItems"][0]["mall_remark"]
except requests.RequestException as e:
print(f"请求尝试 {attempt} 失败: {e}")
except KeyError as e:
print(f"响应中缺少预期字段: {e}")
return None # 返回空值表示请求失败脚本太大只能通过网盘分享了
通过网盘分享的文件:pdd.js
链接: https://pan.baidu.com/s/1iLoJchrE1VKxR5j5Vk5KbA?pwd=xd1p 提取码: xd1p
如果有用记得给博主点个赞