我正在尝试使用Python Requests库下载csv文件 . 我正在使用Requests库,因为我首先必须通过免责声明页面,所以我使用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

我还应该提一下,我也尝试通过chunking返回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()

但我仍然只获得report.csv中的前6行 .

我认为我的内容没有完全加载,因为我在请求 Headers 中遗漏了一些内容 . 这是(工作)浏览器请求标头:

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请求的请求标头(我的响应缺少Transfer-Encoding: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响应头(我失去了Transfer-Encoding:chunked):

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

有谁知道如何发出一个返回整个csv文件的POST请求?