Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用requests.post执行时,响应标题与浏览器不同_Python_Python 3.x_Python Requests - Fatal编程技术网

Python 使用requests.post执行时,响应标题与浏览器不同

Python 使用requests.post执行时,响应标题与浏览器不同,python,python-3.x,python-requests,Python,Python 3.x,Python Requests,使用Python请求库执行以下HTTP请求时,响应如下所示: headers = { 'Host': 'cgaxis.com', 'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:71.0) Gecko/20100101 Firefox/71.0', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'Accept-Langu

使用Python请求库执行以下HTTP请求时,响应如下所示:

headers = {
'Host': 'cgaxis.com',
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:71.0) Gecko/20100101 Firefox/71.0',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language': 'en-US,en;q=0.5',
'Accept-Encoding': 'gzip, deflate, br',
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': '489',
'Origin': 'https://cgaxis.com',
'TE':'Trailers',
'Connection': 'keep-alive',
'Referer': 'https://cgaxis.com/product/christmas-table-decoration-3/',
'Cookie': '_ga=GA1.2.1349896001.1573562165; _gid=GA1.2.1103188066.1573562165; _fbp=fb.1.1573562177263.1142673466; wordpress_logged_in_d654bb6ee10d524ec9b993121bc1de21=b9s%7C1573735076%7CaAPERAC1MmoIXqoQf305isXZa2HkK5owmBgZLK2yupj%7C718ee60eaf0dbaf85ffa2f6402becac567766fbdb08be1f3ac8c99b446907ecf; __stripe_mid=14074cc0-abe8-497d-93e3-329937272fbd; wfwaf-authcookie-4f48e79d1a332c22b28650e533988cc5=13050%7Csubscriber%7C9e787a4d89f8e0ae9354672ae69bd3e3162c38d316095512c8983cd6d17aebc5; AMP_TOKEN=%24NOT_FOUND; __stripe_sid=ffed8ccc-0e78-4967-9ad8-6fa8fcef9fad; cookie_notice_accepted=true; _gat=1',
'Upgrade-Insecure-Requests':'1'
}

data = {'body':'somdn_download_key=MTU3MzYyMzE5Ng%3D%3D&billing_first_name=Benjamin&billing_last_name=Gallais&billing_company=Dassault+Syst%C3%A8mes+SE&billing_country=FR&billing_address_1=10%2C+Rue+Marcel+Dassault+CS+40501&billing_address_2=&billing_postcode=78946&billing_city=VELIZY+VILLACOUBLAY+CEDEX&billing_state=&billing_phone=%2B33+1+55+35+26+27&billing_email=b9s%403ds.com&vat_number=FR52322306440&customer_location_self_certified=&payment_method=&action=somdn_download_single&somdn_product=16950'}

response = requests.post('https://cgaxis.com/product/christmas-table-decoration-3/', data=data, headers=headers, allow_redirects=True)

response.status_code,response.headers
我得到的答复是:

状态代码=200

响应标题:

content-encoding →gzip
content-type →text/html; charset=UTF-8
date →Wed, 13 Nov 2019 06:46:08 GMT
link →<https://cgaxis.com/wp-json/>; rel="https://api.w.org/", <https://cgaxis.com/?p=16950>; rel=shortlink
server →nginx
status →200
vary →Accept-Encoding
x-content-type-options →nosniff
请求有效负载:

POST /product/christmas-table-decoration-1/ HTTP/1.1
Host: cgaxis.com
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:71.0) Gecko/20100101 Firefox/71.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate, br
Referer: https://cgaxis.com/product/christmas-table-decoration-1/
Content-Type: application/x-www-form-urlencoded
Content-Length: 489
Origin: https://cgaxis.com
Connection: keep-alive
Cookie: __stripe_mid=522a5f57-0570-42de-9a74-44c3b4d82f13; wordpress_logged_in_d654bb6ee10d524ec9b993121bc1de21=b9s%7C1573728390%7CpjCzZcjGFKhyJUvMdYxADvPencBDvQNqv4H1Pqhfhky%7Cedd40cfc2f3f323738c74e28d0209c3ed05f7874f1129bb5700b1ff127f43e84
Upgrade-Insecure-Requests: 1
somdn_download_key=MTU3MzU2MDEzNQ%3D%3D&billing_first_name=Benjamin&billing_last_name=Gallais&billing_company=Dassault+Syst%C3%A8mes+SE&billing_country=FR&billing_address_1=10%2C+Rue+Marcel+Dassault+CS+40501&billing_address_2=&billing_postcode=78946&billing_city=VELIZY+VILLACOUBLAY+CEDEX&billing_state=&billing_phone=%2B33+1+55+35+26+27&billing_email=b9s%403ds.com&vat_number=FR52322306440&customer_location_self_certified=&payment_method=&action=somdn_download_single&somdn_product=16686
HTTP/2.0 302 Found
server: nginx
date: Wed, 13 Nov 2019 06:16:58 GMT
content-type: text/html; charset=UTF-8
access-control-allow-origin: https://cgaxis.com
access-control-allow-credentials: true
vary: Origin
location: https://cgaxis.ams3.digitaloceanspaces.com/amz/Volume-039/single_models/cgaxis_models_volume_39_20.zip?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=JJXE3XDR6KI4A3AMOVSE%2F20191113%2Feu-central-1%2Fs3%2Faws4_request&X-Amz-Date=20191113T061658Z&X-Amz-Expires=1200&X-Amz-SignedHeaders=host&X-Amz-Signature=1aa97d6443cd1861b2b9e03e32dcebaf4fd59e3293b99153b41534a030207975
x-content-type-options: nosniff
X-Firefox-Spdy: h2
响应标题:

POST /product/christmas-table-decoration-1/ HTTP/1.1
Host: cgaxis.com
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:71.0) Gecko/20100101 Firefox/71.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate, br
Referer: https://cgaxis.com/product/christmas-table-decoration-1/
Content-Type: application/x-www-form-urlencoded
Content-Length: 489
Origin: https://cgaxis.com
Connection: keep-alive
Cookie: __stripe_mid=522a5f57-0570-42de-9a74-44c3b4d82f13; wordpress_logged_in_d654bb6ee10d524ec9b993121bc1de21=b9s%7C1573728390%7CpjCzZcjGFKhyJUvMdYxADvPencBDvQNqv4H1Pqhfhky%7Cedd40cfc2f3f323738c74e28d0209c3ed05f7874f1129bb5700b1ff127f43e84
Upgrade-Insecure-Requests: 1
somdn_download_key=MTU3MzU2MDEzNQ%3D%3D&billing_first_name=Benjamin&billing_last_name=Gallais&billing_company=Dassault+Syst%C3%A8mes+SE&billing_country=FR&billing_address_1=10%2C+Rue+Marcel+Dassault+CS+40501&billing_address_2=&billing_postcode=78946&billing_city=VELIZY+VILLACOUBLAY+CEDEX&billing_state=&billing_phone=%2B33+1+55+35+26+27&billing_email=b9s%403ds.com&vat_number=FR52322306440&customer_location_self_certified=&payment_method=&action=somdn_download_single&somdn_product=16686
HTTP/2.0 302 Found
server: nginx
date: Wed, 13 Nov 2019 06:16:58 GMT
content-type: text/html; charset=UTF-8
access-control-allow-origin: https://cgaxis.com
access-control-allow-credentials: true
vary: Origin
location: https://cgaxis.ams3.digitaloceanspaces.com/amz/Volume-039/single_models/cgaxis_models_volume_39_20.zip?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=JJXE3XDR6KI4A3AMOVSE%2F20191113%2Feu-central-1%2Fs3%2Faws4_request&X-Amz-Date=20191113T061658Z&X-Amz-Expires=1200&X-Amz-SignedHeaders=host&X-Amz-Signature=1aa97d6443cd1861b2b9e03e32dcebaf4fd59e3293b99153b41534a030207975
x-content-type-options: nosniff
X-Firefox-Spdy: h2

我需要上述响应中的
location
参数,以便与
urllib.request.urlretrieve()一起使用


有人能帮忙吗?

如果你想得到相同的回复,请确保你也发送了相同的请求

来自浏览器的响应可能是选项请求的结果,而不是POST。您可以在浏览器内的“网络”选项卡中看到这一点。发布第二次响应的原始请求可以让我们为您提供更多帮助


还要知道,web服务器可以对每个用户代理有不同的响应。尝试在pythov代码中设置用户代理标头,使其看起来像是来自浏览器。

从浏览器调用时,它看起来像是在重定向。 因此,为了在从python库调用时模拟相同的行为,应该使用allow_redirects=False参数

response = requests.post('https://cgaxis.com/product/christmas-table-decoration-3/', data=data, headers=headers, allow_redirects=False)

这将确保不会自动重定向到“位置”属性。在此之后,您可以从响应头中提取location属性并进行后续调用

你是在问如何不遵循重定向吗?如果您不想跟随重定向,以便可以使用位置URL跟随重定向,为什么?另外,为什么同时使用requests和urllib?
urllib.request.urlretrieve()
是在不使用浏览器的情况下自动下载文件。我正在使用请求库发布带有特定正文和标题的请求。我是python初学者……如果有更好的方法,请告诉我设置
allow\u redirects=False
会给出相同的响应。状态代码和响应标题均为sameI。我已编辑问题以从浏览器添加请求和响应标题以及请求有效负载。用户代理的设置也与浏览器请求头中的设置相同。在网络选项卡中,它被称为POST请求,状态代码为302