尝试将发帖请求发送到https://www.off---white.com/en/IT/orders/populate.json以便向购物车添加以下页面上的项目时,出现“ 403禁止”错误:https://www.off---white.com/en/IT/women/products/owea143e18a640731001

这是我的代码:

import cfscrape
scrape = cfscrape.create_scraper()
payload = {"variant_id": "107917", "quantity": "1"}
headers={
    "accept":"application/json, text/javascript, */*; q=0.01",
    "accept-encoding": "gzip, deflate, br",
    "accept-language": "en-US,en;q=0.9",
    "content-length": "35",
    "content-type": "application/json; charset=UTF-8",
    "refer": "https://www.off---white.com/en/IT/women/products/owea143e18a640731001",
    "origin":"https://www.off---white.com",
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36",
    "x-requested-with": "XMLHttpRequest"
    }

res = scrape.post("https://www.off---white.com/en/IT/orders/populate.json",headers=headers,data=payload)
print(res.status_code)  #=403 :(


谢谢。

编辑:显然我需要cookie。我按如下所示更改了代码,但是我仍然被禁止使用403。

cookie, user_agent=scrape.get_cookie_string("https://www.off---white.com/en/IT/women/products/owea143e18a640731001")
headers={
    "authority":"www.off---white.com",
    "accept":"application/json, text/javascript, */*; q=0.01",
    "accept-encoding": "gzip, deflate, br",
    "accept-language": "en-US,en;q=0.9",
    "content-length": "35",
    "content-type": "application/json; charset=UTF-8",
    "refer": "https://www.off---white.com/en/IT/women/products/owea143e18a640731001",
    "origin":"https://www.off---white.com",
    "cookie": cookie,
    "user-agent": user_agent,
    "x-requested-with": "XMLHttpRequest"
    }
res = scrape.post("https://www.off---white.com/en/IT/orders/populate.json",headers=headers,data=payload)


关于仍然缺少的一些技巧?

EDIT2:我也尝试了以下方法:

tokens, user_agent=scrape.get_tokens("https://www.off---white.com/en/IT/women/products/owea143e18a640731001")
headers={
    "authority":"www.off---white.com",
    "accept":"application/json, text/javascript, */*; q=0.01",
    "accept-encoding": "gzip, deflate, br",
    "accept-language": "en-US,en;q=0.9",
    "content-length": "35",
    "content-type": "application/json; charset=UTF-8",
    "refer": "https://www.off---white.com/en/IT/women/products/owea143e18a640731001",
    "origin":"https://www.off---white.com",
    "user-agent": user_agent,
    "x-requested-with": "XMLHttpRequest"
    }
res = scrape.post("https://www.off---white.com/en/IT/orders/populate.json",headers=headers,data=payload,cookies=tokens)


再次,没有运气。另外,无论我如何提交cookie,我都注意到无论何时获得请求,该请求都会立即被拒绝,cfscrape的node.js窗口甚至都不会打开。

最佳答案

您确定Content-Length是正确的吗?那可能是一个问题。通常,您不需要包含此内容,以后它会自己添加。另一个问题很可能是cookie。我看不到您将POST请求发送回cookie。有2个必需的cookie,其中一个是_hs_session_v3,您没有发送。

编辑:
在浏览器中执行以下操作:1. GET请求到off---white.com。保存所有这些cookie。 2.从off---white.com/en/IT/women/products/…获得GET请求。保存这些cookie。 3. POST请求将起作用。要对此进行测试,请执行以下操作:清除所有浏览器缓存/ cookie。转到off---white.com/en/IT/women/products/…,它甚至没有加载页面。为什么不呢?您的Cookie未设置。该网站正在使用您的Cookie来确定您的请求是否有效。您正在请求一个页面,该页面依赖Cookie进行有效响应。

当我单击添加到购物袋时,这是卷曲:curl'https://www.off---white.com/en/IT/orders/populate.json'-H'cookie:__cfduid = db351a20a2752d5459220f241c66440371539900632; dismiss_cookie_law = true; guest_token = Im41TjNZWFBFRFFwaUdvNkpTeVJDZnci--1fca5ea27af50cba12cd96f088975f5f5e0647ac; __riskifiedBeaconSessionId = c0f01056-f52a4b5a-76f25d73-cdb11d12-c2f20dbc-8173a1e0; cf_clearance = eb87f25251aeab5062f65bc7a5261b9cbef35809-1539947407-1800-150; _hs_session_v3 = UHRUWUtUVDJKTTExeEkxTVNQVlFMSVQySUR1VXlZZFJGaGZJck0zOTBPbVZSWjUvL01pR2J1UktjOXFPMTZiK2pVbm5jVGtkUU9GRVpJVGR0VWkxZGUyWGNvak5kZXllR2tEMG5yOERHMmVMaHJxaUxYRHRGVUp3NXAvZERGSm9nREZBc0Z0bDJsM241VHhzdU16S05WT3BUaFlNbVhwYkd3cGNjdHAzSHpRPS0tZ2pOR25tNzJMTWlTY3U1MDJTaW9iQT09--44bbe09a45974b9e7584588b186650946c1b6f73' -H '起源:https://www.off---white.com' -H '接受编码:gzip,DEFLATE,BR' -H '接受语言:EN-US,EN; Q = 0.9' -H“× -requested-with:XMLHttpRequest'-H'pragma:no-cache'-H'用户代理:Mozilla / 5.0(Windows NT 6.1; Win64; x64)AppleWebKit / 537.36(KHTML,like Gecko)Chrome / 69.0.3497.100 Safari /537.36'-H'内容类型:application / json; charset = UTF-8'-H'接受:application / json,text / javascript,/; q = 0.01'-H'缓存控制:无缓存'-H'权限:www.off---white.com'-H'推荐人:https://www.off---white.com/en/IT/women/products/owea143e18a640731001'-H'dnt:1'--data-二进制'{“ variant_id”:null,“ quantity”:1}'-压缩

关于python - 禁止使用CloudFlare Scrape 403,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52883064/

10-12 19:06