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

curl可调用Vinted API但Python Requests返回403,疑遭Datadome/Cloudflare拦截

解决Requests调用vinted.nl接口返回403的问题

以下是几个能排查和解决该问题的实用方向:

1. 1:1复刻curl的请求头与Cookie

浏览器和curl能成功请求,说明核心参数是有效的,问题大概率出在Requests的请求头、Cookie传递与curl不完全匹配上:

  • 先在终端执行带Cookie的curl命令并查看完整请求细节:curl -v -b "你的完整Cookie内容" https://www.vinted.nl/api/v2/languages
  • 将输出里的所有请求头原封不动复制到Requests中,不要保留任何Requests默认添加的额外头。示例代码:
import requests

# 完全照搬curl的请求头
headers = {
    'User-Agent': 'curl/7.81.0',  # 替换成你本地curl的实际版本
    'Accept': '*/*',
    'Accept-Encoding': 'gzip, deflate, br',
    'Connection': 'keep-alive',
    # 把curl输出里的其他请求头全部补充进来
}

# 逐个复制浏览器/Cookie中的键值对
cookies = {
    'vinted_fr': 'xxx',
    'datadome': 'xxx',
    # 所有Cookie字段都不能遗漏
}

response = requests.get('https://www.vinted.nl/api/v2/languages', headers=headers, cookies=cookies)
print(response.status_code)
print(response.text)

2. 使用Session维持会话状态

单独的GET请求可能无法维持服务器端的会话关联,用requests.Session()可以自动管理Cookie,更贴近浏览器的会话逻辑:

import requests

session = requests.Session()

# 先访问主页获取完整的会话Cookie
session.get('https://www.vinted.nl/', headers={
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36'
})

# 再调用接口,Session会自动带上之前获取的所有Cookie
response = session.get('https://www.vinted.nl/api/v2/languages', headers={
    'Accept': 'application/json',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36'
})
print(response.status_code)

3. 检查是否遗漏反爬验证参数

Cloudflare 1020错误是访问权限被拒,可能是缺少动态生成的验证参数:

  • 打开浏览器开发者工具,查看调用/api/v2/languages时的所有请求头和Cookie,确保Requests中完全一致,包括Referer、Origin这类容易被忽略的字段。
  • 部分反爬机制会校验请求头的顺序,Python 3.7+字典默认有序,直接按浏览器中的头顺序构造headers即可。

4. 禁用自动重定向

Requests默认的自动重定向可能导致Cookie丢失或验证流程中断,可尝试关闭重定向:

response = requests.get(url, headers=headers, cookies=cookies, allow_redirects=False)

内容的提问来源于stack exchange,提问作者Sander van den Oord

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.15 23:52:31