Python post请求返回响应400
Posted
技术标签:
【中文标题】Python post请求返回响应400【英文标题】:Python post requests return response 400 【发布时间】:2021-07-10 07:47:33 【问题描述】:[
我正在尝试抓取此网站https://www.kickstarter.com/projects/818583073/dies-irae-day-of-wrath-rated-r/,当我向其 API 发送帖子请求时,它只返回响应 400 和空内容。
这是我的代码。
import requests
url = 'https://www.kickstarter.com/graph'
headers = 'authority':'www.kickstarter.com',
'method':'POST',
'path':'/graph',
'scheme':'https',
'accept':'*/*',
'accept-encoding':'gzip, deflate, br',
'accept-language':'en-US,en;q=0.9',
'content-length':'606',
'content-type':'application/json',
'cookie':"vis=f5761fb0e1994852-b38b5b3d46161036-c3a4a56c5add1076v1; lang=en; woe_id=YzFrZ1NUV1lRTUhMT2tsc1ZURHVsQT09LS12L0pidVVCeDBHZU16dk81MmVpeTNBPT0%3D--468e7c1e5daf8c17cdd902b0a1cb1ef4e2856543; optimizely_current_variations=%7B%7D; _pxhd=75f70796791b6f8a5930b19c70bcd30d268fe4a4f1644460c7c7bbe65d5e8196:837ba981-9d56-11eb-841e-e7065f1f0101; _pxvid=837ba981-9d56-11eb-841e-e7065f1f0101; ajs_anonymous_id=%22f5761fb0e1994852-b38b5b3d46161036-c3a4a56c5add1076v1%22; _ga=GA1.2.17378398.1618428050; _gid=GA1.2.1258279558.1618428050; __ssid=3d59a55ffedce2904d3464e3a555309; em_cdn_uid=t%3D1618428051657%26u%3D8d620439ed7740b89c98770bbaee8b05; __stripe_mid=e4e89c20-83c7-4ba0-907b-7b83f8b24051e87f22; em_p_uid=l:1618428053354|t:1618428053353|u:c814f9e5a157438b910a57075a7fe320; __stripe_sid=eaa7f9e2-2ba2-45db-8213-c79be847d1100aa907; ajs_anonymous_id=%22f5761fb0e1994852-b38b5b3d46161036-c3a4a56c5add1076v1%22; _px2=eyJ1IjoiZTQ0MTZhZjAtOWRjNi0xMWViLWE0M2MtYjM2ODk4NjhlZTYxIiwidiI6IjgzN2JhOTgxLTlkNTYtMTFlYi04NDFlLWU3MDY1ZjFmMDEwMSIsInQiOjE2MTg0NzY4NTU0MjQsImgiOiIwYzI2NWVlOTAzY2U1NWM1MGI4ODhmZTFkZmMxYTZhYmMzYmVkYzU5ZDQ4OTA3ZDg4NGEzZjA0NDBmN2RjNTczIn0=; last_page=https%3A%2F%2Fwww.kickstarter.com%2Fprojects%2F818583073%2Fdies-irae-day-of-wrath-rated-r%2Fdescription; _ksr_session=THQxSGRqa3FabnRmV1V5Wk9sbStaV1ltSWlTcTIzc3hBTFJYeXN0TnpTMEJYK2lZN3VIS3pTakhkeTlRL2VxNWJNT2svZkFabjFNeFB6aG1YaEwrNjR6WCtIZGxUamF2TEdOK2YvOS8yQjJseWtvajVtUHZDcVBoa3JVOTBHSFFoSjg4WFdzUllRblJubUM0bnVlak9nPT0tLVlXS1M5R3pYNVZtcjllZklGVWNiL2c9PQ%3D%3D--962d5221d168b9e73b4769f2a39314c747fe24bf; request_time=Thu%2C+15+Apr+2021+08%3A52%3A59+-0000; local_offset=-2333",
'origin':'https://www.kickstarter.com',
'referer':'https://www.kickstarter.com/projects/818583073/dies-irae-day-of-wrath-rated-r/description',
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (Khtml, like Gecko) Chrome/89.0.4389.114 Safari/537.36',
'x-csrf-token':'tQoXSUK0yYY+5r6bgZH3i3Pfya96S6S6ZcOmh/6qRea0XLl1c6ZX6Asmm3vrtenqxOKacQi1tKTN0c9OxRJyoA=='
payload =
'operationName':'Campaign',
'query':'query Campaign($slug: String!) ↵ project(slug: $slug) ↵ id↵ isSharingProjectBudget↵ risks↵ story(assetWidth: 680)↵ currency↵ spreadsheet ↵ displayMode↵ public↵ url↵ data ↵ name↵ value↵ phase↵ rowNum↵ __typename↵ ↵ dataLastUpdatedAt↵ __typename↵ ↵ environmentalCommitments ↵ id↵ commitmentCategory↵ description↵ __typename↵ ↵ __typename↵ ↵↵',
'variables':
'slug':'818583073/dies-irae-day-of-wrath-rated-r'
r = requests.post(url, headers=headers, data=payload)
print(r.status_code)
print(r.content)
400
''
【问题讨论】:
x-csrf-token
标头对于每个请求可能必须不同...
【参考方案1】:
您已在标题中将内容类型设置为 json
把它改成'Content-Type':'text/html'
就可以了
【讨论】:
它只返回这个 'errors': ['message': 'No query string was present'] @MalikIbrahim 现在错误的请求错误已解决。您必须使用查询调试并准备适当的有效负载,然后重试。您可能需要查看此特定 API 的 API 文档 你能帮我吗?我试过了,但没有找到。以上是关于Python post请求返回响应400的主要内容,如果未能解决你的问题,请参考以下文章
Python请求 - 无法获得POST表单响应(仅返回表单)
400 仅在启用 cors 时响应 asp.net web api POST 请求