我无法获得完整的xls文件,只有python3请求提供的表头

问题描述 投票:0回答:1

我想从website导出报告,当我单击导出按钮时,它将下载一个xls文件。

首先,在邮递员中,我可以获得竞争报告postman response result img

这是邮递员中的响应头

Cache-Control: no-cache
Cache-Control: no-store
content-disposition: attachment;filename=U3sdad.xls
Content-Language: zh-CN
Date: Tue, 08 Oct 2019 10:17:24 GMT
Expires: Thu, 01 Jan 1970 00:00:00 GMT
Pragma: no-cache
Server: Apache-Coyote/1.1
Transfer-Encoding: chunked

但是当我使用resquests时,我只是在xls文件中获得表头。我尝试使用2种方式获取xls

1:

#I ues session,because i need cookie to login
s = requests.session()
report_response = s.get(url=report_url, params=params)
with open('1.xls','wb') as f:
    f.write(report_response.content)

2:

#I ues session,because i need cookie to login
s = requests.session()
report_response = s.get(url=report_url, params=params,stream=True,allow_redirects=True)
with open('1.xls','wb') as f:
    for chunk in report_response.iter_content(chunk_size=512):
        if chunk:
           f.write(chunk)

但是他们无法获得像邮递员那样的竞争性xls文件。我不知道为什么,我也没有在Google中找到解决方案

python excel get python-requests xls
1个回答
0
投票

尝试

import requests
dls = "http://url/path/to.xls"
resp = requests.get(dls)

output = open('test.xls', 'wb')
output.write(resp.content)
output.close()

或此

import requests
dls = "http://url/path/to.xls"
resp = requests.get(dls)
with open('test.xls', 'wb') as output:
    output.write(resp.content)

都应该起作用。

© www.soinside.com 2019 - 2024. All rights reserved.