与request.post一起执行时,响应标头与浏览器不同

时间:2019-11-13 07:21:49

标签: 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-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

我得到的答复是:

status code = 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

我需要以上响应中的location参数才能与urllib.request.urlretrieve()一起使用。

有人可以帮忙吗?

2 个答案:

答案 0 :(得分:0)

如果您希望获得相同的响应,请确保您也发送相同的请求。

浏览器的响应可能是OPTION请求的结果,而不是POST。您可以在浏览器内部的“网络”标签中看到此内容。发布第二个回复的原始请求可让我们为您提供更多帮助

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

答案 1 :(得分:0)

当您从浏览器中调用它时,似乎正在重定向。 因此,要在从python库进行调用时模仿相同的行为,应使用allow_redirects = False参数。

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

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