You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Magento网站爬取问题:getequivalentproducts API Timestamp无效致空响应

解决Prolians网站等价商品API反爬限制的方案

核心问题分析

你碰到的是Cloudflare+Magento的会话绑定反爬机制:浏览器访问商品页时,Cloudflare会生成会话级验证令牌(如cf_clearance Cookie),同时Magento会关联该会话的请求标识,只有和有效浏览器会话绑定的请求才能拿到有效响应。直接复制Postman请求能成功,是因为它带了当时浏览器的完整Cookie和请求头。

具体解决步骤

1. 复用浏览器会话的Cookie与请求头

  • 打开浏览器访问目标商品页,按F12打开开发者工具的「网络」标签,找到getequivalentproducts请求,复制全部请求头和Cookie
  • 在Python请求中完全复用这些内容,包括User-Agent、Referer、cf_clearance、__cf_bm等Cloudflare相关Cookie,以及Magento的会话Cookie(如PHPSESSID)
  • 示例代码:
import requests

url = "https://www.prolians.fr/equivalentproducts/widget/getequivalentproducts?sku=P702708&_=1726650753165"
headers = {
    "User-Agent": "你的浏览器真实User-Agent",
    "Referer": "https://www.prolians.fr/对应商品页的完整URL",
    # 粘贴复制到的其他所有请求头
}
cookies = {
    "cf_clearance": "你复制的cf_clearance值",
    "__cf_bm": "你复制的__cf_bm值",
    "PHPSESSID": "你复制的会话ID",
    # 粘贴复制到的其他所有Cookie
}

response = requests.get(url, headers=headers, cookies=cookies)
print(response.json())

2. 用无头浏览器模拟完整访问流程

如果Cookie过期频繁,用Playwright或Selenium模拟浏览器行为:

  • 先访问商品页,等待页面加载完成(确保Cloudflare验证通过)
  • 直接从浏览器网络请求中提取API响应,或构造请求复用当前会话
  • 示例Playwright代码:
from playwright.sync_api import sync_playwright

with sync_playwright() as p:
    browser = p.chromium.launch(headless=True)
    page = browser.new_page()
    # 先访问商品页,完成Cloudflare验证
    page.goto("https://www.prolians.fr/对应商品页的完整URL")
    page.wait_for_load_state("networkidle")
    
    # 用浏览器内置方法生成时间戳,构造API请求并复用会话
    timestamp = int(page.evaluate("Date.now()"))
    api_url = f"https://www.prolians.fr/equivalentproducts/widget/getequivalentproducts?sku=P702708&_={timestamp}"
    response = page.request.get(api_url)
    print(response.json())
    
    browser.close()

3. 解析Cloudflare验证逻辑(进阶)

若需长期自动化,可分析Cloudflare的验证流程:

  • 观察浏览器首次访问时的JS验证挑战,提取验证参数
  • 用Python模拟JS验证逻辑,生成有效的cf_clearance Cookie
  • 注意:Cloudflare的验证逻辑会频繁更新,需定期维护

注意事项

  • 所有请求必须和商品页会话保持一致,Referer必须指向对应商品页
  • 控制请求频率,避免触发Cloudflare的IP封禁
  • 个人用途请遵守网站的使用条款与robots.txt规则

内容的提问来源于stack exchange,提问作者Robin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 10:17:10