Python请求POST返回不完整的内容



我正在尝试使用Python请求库下载一个csv文件。我使用请求库是因为我首先必须通过免责声明页面,所以我使用Session对象来存储所有cookie和所有爵士乐。我的POST请求不断返回,返回的响应内容仅为csv文件的前6行。当我使用浏览器下载该文件时,它有1622行长。我当前的脚本:

import logging
logging.basicConfig(level=logging.DEBUG)
import pdb
import requests
s = requests.Session()
## Disclaimer page session
dis_url = 'http://a100.gov.bc.ca/pub/gwl/disclaimer.do'
accept_form = {'submitType':'Accept'}
s.post(dis_url, data=accept_form)

## POST request
base_url = 'http://a100.gov.bc.ca/pub/gwl/plot.do'
postContent = {
'fromYear':'2012',
'fromMonth':'1',
'fromDay':'1',
'toYear':'2013',
'toMonth':'1',
'toDay':'1',
'emsIDs':'E290172' ,
'mode':'GRAPH',
'mmaFlags':'false',    
'submitType':'Download'}

httpHeaders = {
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Host': 'a100.gov.bc.ca',
'Connection': 'keep-alive',
'Content-Length': '155',
'User-Agent': 'python-requests/1.2.3 CPython/2.7.3 Linux/3.5.0-23-generic',
'Content-Type': 'application/x-www-form-urlencoded',
'Referer': 'http://www.env.gov.bc.ca/wsd/data_searches/obswell/map/obsWells.html',
'Accept-Encoding': 'gzip,deflate,sdch',
'Accept-Language': 'en-US,en;q=0.8'}

r = s.post(base_url, data=postContent, headers=httpHeaders, stream=False, timeout=3600)
print r.content

我还应该提到,我也尝试过通过分块返回csv,比如:

with open("report.csv",'wb') as file:
    r = s.post(base_url,stream=True,timeout=3600, data=postContent, headers=httpHeaders)
    for chunk in r.iter_content(chunk_size=1024):
        if chunk:
            file.write(chunk)
            file.flush()

但我仍然只得到报告中的前6行。csv.

我认为我的内容没有完全加载,因为我的请求标头中缺少一些内容。这是(正在工作的(浏览器请求标头:

POST /pub/gwl/plot.do HTTP/1.1
Host: a100.gov.bc.ca
Connection: keep-alive
Content-Length: 155
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Origin: http://www.env.gov.bc.ca
User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36
Content-Type: application/x-www-form-urlencoded
Referer: http://www.env.gov.bc.ca/wsd/data_searches/obswell/map/obsWells.html
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.    
Cookie: JSESSIONID=73de9312c8dcf1c4c657d19adbe811b88792479fe72eb2e2feeedea7d88bdbf8.e3uMah8KbhmLe3mMbN8Pa3uPbi1ynknvrkLOlQzNp65In0; WT_FPC=id=2fcd604924a9af3c13e1374599088181:lv=1383612362138:ss=1383612301792

以及工作浏览器响应标头:

HTTP/1.1 200 OK
Date: Mon, 04 Nov 2013 20:59:52 GMT
Server: Oracle-Application-Server-10g/10.1.2.2.0 Oracle-HTTP-Server
Content-Disposition: attachment; filename="gwl_report.csv"
Cache-Control: must-revalidate
Content-Type: application/download
Set-Cookie: JSESSIONID=61d874e1b5ce07df96aaabe504d7c18788e5aaf773a7bee7ab4b0cf349a88aaa.e3uMah8KbhmLe3mMbN8Pa3uPbi1ynknvrkLOlQzNp65In0; path=/pub/gwl
Transfer-Encoding: chunked

python post请求的请求头(我的响应缺少传输编码:chunked(:

Content-Length : 126
Accept-Language : en-US,en;q=0.8
Accept-Encoding : gzip,deflate,sdch
Connection : keep-alive
Accept : text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
User-Agent : python-requests/1.2.3 CPython/2.7.3 Linux/3.5.0-23-generic
Host : a100.gov.bc.ca
Referer : http://www.env.gov.bc.ca/wsd/data_searches/obswell/map/obsWells.html
Cookie : JSESSIONID=9a51e637cccc6164e4784631ef9a0ab21574c518c1c5c86cf0892bbf2aa22c95.e3uMah8KbhmLe3mMbN8Pa3uPbi1ynknvrkLOlQzNp65In0
Content-Type : application/x-www-form-urlencoded

Python响应头(我的缺少传输编码:分块(:

content-length : 200
content-disposition : attachment; filename="gwl_report.csv"
set-cookie : JSESSIONID=9a51e637cccc6164e4784631ef9a0ab21574c518c1c5c86cf0892bbf2aa22c95.e3uMah8KbhmLe3mMbN8Pa3uPbi1ynknvrkLOlQzNp65In0; path=/pub/gwl
server : Oracle-Application-Server-10g/10.1.2.2.0 Oracle-HTTP-Server
cache-control : must-revalidate
date : Tue, 05 Nov 2013 00:51:47 GMT
content-type : application/download

有人知道我如何发出POST请求,返回整个csv文件吗?

它可能在请求和urllib3中出现错误https://github.com/psf/requests/issues/4956
另请参阅:https://blog.petrzemek.net/2018/04/22/on-incomplete-http-reads-and-the-requests-library-in-python/

相关内容

  • 没有找到相关文章

最新更新