Python 请求未返回与浏览器相同 Header Request/cURL
Python Requests Not Returning Same Header as Browser Request/cURL
我正在寻找可以自动从 Bureau of Transportation Statistics Carrier Website 下载 .zip
文件的脚本,但我无法获得相同的响应 header下载 zip 文件时在 Chrome 中看到。我希望收到这样的回复 header:
HTTP/1.1 302 Object moved
Cache-Control: private
Content-Length: 183
Content-Type: text/html
Location: http://tsdata.bts.gov/103627300_T_T100_SEGMENT_ALL_CARRIER.zip
Server: Microsoft-IIS/8.5
X-Powered-By: ASP.NET
Date: Thu, 21 Apr 2016 15:56:31 GMT
但是,当使用我在 Chrome 网络检查器中看到的相同信息调用 requests.post(url, data=params, headers=headers)
时,我得到以下响应:
>>> res.headers
{'Cache-Control': 'private', 'Content-Length': '262', 'Content-Type': 'text/html', 'X-Powered-By': 'ASP.NET', 'Date': 'Thu, 21 Apr 2016 20:16:26 GMT', 'Server': 'Microsoft-IIS/8.5'}
除了缺少 Location
密钥外,它几乎拥有所有内容,我需要它才能下载包含我想要的所有数据的 .zip
文件。 Content-Length
值也不同,但我不确定这是否是个问题。
我认为我的问题与以下事实有关:当您在页面上单击 "Download" 时,它实际上发送了两个我可以在 Chrome 网络控制台中看到的请求。第一个请求是一个 POST
请求,它产生一个 HTTP
响应 302,然后在响应 header 中有 Location
。第二个请求是对 url 的 GET
请求,在响应 header 的 Location
值中指定。
我真的应该在这里发送两个请求吗?为什么我使用 requests
时没有得到与在浏览器中相同的响应 headers? FWIW 我使用 curl -X POST -d /*my data*/
并在我的终端中取回了这个:
<head><title>Object moved</title></head>
<body><h1>Object Moved</h1>This object may be found <a HREF="http://tsdata.bts.gov/103714760_T_T100_SEGMENT_ALL_CARRIER.zip">here</a>.</body>
非常感谢任何帮助!
我能够使用 几乎 我在 Google 中看到的所有 headers 下载我正在寻找的 zip 文件Chrome 网络控制台。我的 headers 看起来像这样:
{'Connection': 'keep-alive', 'Cache-Control': 'max-age=0', 'Referer': 'http://www.transtats.bts.gov/DL_SelectFields.asp?Table_ID=293', 'Origin': 'http://www.transtats.bts.gov', 'Upgrade-Insecure-Requests': 1, 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8', 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.112 Safari/537.36', 'Cookie': 'ASPSESSIONIDQADBBRTA=CMKGLHMDDJIECMNGLMDPOKHC', 'Accept-Language': 'en-US,en;q=0.8', 'Accept-Encoding': 'gzip, deflate', 'Content-Type': 'application/x-www-form-urlencoded'}
然后我就写了:
res = requests.post(url, data=form_data, headers=headers)
其中 form_data
是从 Chrome 控制台的 "Form Data" 部分复制的。收到该请求后,我使用 zipfile
和 io
模块来解析存储在 res
中的响应内容。像这样:
import zipfile, io
zipfile.ZipFile(io.BytesIO(res.content))
然后文件位于我 运行 Python 代码所在的目录中。
感谢在 this thread 上回答的用户。
我正在寻找可以自动从 Bureau of Transportation Statistics Carrier Website 下载 .zip
文件的脚本,但我无法获得相同的响应 header下载 zip 文件时在 Chrome 中看到。我希望收到这样的回复 header:
HTTP/1.1 302 Object moved
Cache-Control: private
Content-Length: 183
Content-Type: text/html
Location: http://tsdata.bts.gov/103627300_T_T100_SEGMENT_ALL_CARRIER.zip
Server: Microsoft-IIS/8.5
X-Powered-By: ASP.NET
Date: Thu, 21 Apr 2016 15:56:31 GMT
但是,当使用我在 Chrome 网络检查器中看到的相同信息调用 requests.post(url, data=params, headers=headers)
时,我得到以下响应:
>>> res.headers
{'Cache-Control': 'private', 'Content-Length': '262', 'Content-Type': 'text/html', 'X-Powered-By': 'ASP.NET', 'Date': 'Thu, 21 Apr 2016 20:16:26 GMT', 'Server': 'Microsoft-IIS/8.5'}
除了缺少 Location
密钥外,它几乎拥有所有内容,我需要它才能下载包含我想要的所有数据的 .zip
文件。 Content-Length
值也不同,但我不确定这是否是个问题。
我认为我的问题与以下事实有关:当您在页面上单击 "Download" 时,它实际上发送了两个我可以在 Chrome 网络控制台中看到的请求。第一个请求是一个 POST
请求,它产生一个 HTTP
响应 302,然后在响应 header 中有 Location
。第二个请求是对 url 的 GET
请求,在响应 header 的 Location
值中指定。
我真的应该在这里发送两个请求吗?为什么我使用 requests
时没有得到与在浏览器中相同的响应 headers? FWIW 我使用 curl -X POST -d /*my data*/
并在我的终端中取回了这个:
<head><title>Object moved</title></head>
<body><h1>Object Moved</h1>This object may be found <a HREF="http://tsdata.bts.gov/103714760_T_T100_SEGMENT_ALL_CARRIER.zip">here</a>.</body>
非常感谢任何帮助!
我能够使用 几乎 我在 Google 中看到的所有 headers 下载我正在寻找的 zip 文件Chrome 网络控制台。我的 headers 看起来像这样:
{'Connection': 'keep-alive', 'Cache-Control': 'max-age=0', 'Referer': 'http://www.transtats.bts.gov/DL_SelectFields.asp?Table_ID=293', 'Origin': 'http://www.transtats.bts.gov', 'Upgrade-Insecure-Requests': 1, 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8', 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.112 Safari/537.36', 'Cookie': 'ASPSESSIONIDQADBBRTA=CMKGLHMDDJIECMNGLMDPOKHC', 'Accept-Language': 'en-US,en;q=0.8', 'Accept-Encoding': 'gzip, deflate', 'Content-Type': 'application/x-www-form-urlencoded'}
然后我就写了:
res = requests.post(url, data=form_data, headers=headers)
其中 form_data
是从 Chrome 控制台的 "Form Data" 部分复制的。收到该请求后,我使用 zipfile
和 io
模块来解析存储在 res
中的响应内容。像这样:
import zipfile, io
zipfile.ZipFile(io.BytesIO(res.content))
然后文件位于我 运行 Python 代码所在的目录中。
感谢在 this thread 上回答的用户。