Python Requests POST 返回不完整的内容

3
我正在尝试使用Python Requests库下载csv文件。我使用Requests库是因为我首先必须通过免责声明页面,所以我使用Session对象来存储所有的cookies等信息。我的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()

但是在report.csv中我只得到了前6行内容。

我认为我的内容没有完全加载,因为我的请求头缺少某些内容。这是一个(有效的)浏览器请求头:

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响应头(我的缺少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

有人知道如何发出POST请求并返回整个CSV文件吗?

为什么你加上了 stream=False 参数? 如果你移除了这个参数会发生什么? - Eevee
2
如果你将它设置为“True”会怎样呢? :) 你在Python中得到了一个Content-Length,但是在浏览器中没有得到,所以我猜这个网站有问题,没有分块就无法工作,但是没有人注意到,因为所有浏览器都支持它。 - Eevee
@Eevee 我也尝试过对响应进行分块,但没有成功。 - hailes
  1. 服务器是否实际只发送了200字节(我猜是6行)的第二个(Python)响应?
  2. 您需要弄清楚为什么它会向浏览器发送“chunked”,而不是发给 Python;我猜是它查看了“User-Agent”——尝试发送与浏览器发送相同的内容。
- Vasiliy Faronov
@vasiliy-faronov,关于你的第二个问题,我使用r.request.headers获取了响应头。 - hailes
显示剩余5条评论
网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接