如何在Python中使用请求导出Excel文件

2024-04-25 08:53:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用Python和Requests包从网页导出Excel文档。你知道吗

注意:如果要单击浏览器中的链接,会出现一个新窗口,要求我打开或保存文件。

我使用请求来“获取”生成弹出窗口的页面,如上所述。你知道吗

此get请求指定类型:vnd.openxmlformats-officedocument.spreadsheetml.sheet

请参见下面的代码:

payload = {'username': 'name@domain.com', 'password': 'thisisit'}
with requests.Session() as s:
    s.post('https://example.com/login', data=payload)
    url = 'https://example.com/inquiry_detail?action=download_inquiry_items&inquiry_id=1'
    r = requests.get(url, allow_redirects = True)
    with open('foobar', 'wb') as f:
        f.write(r.content)

我希望收到excel文件的二进制文件。相反,我只是从重定向中获取html,而不是文件的链接。你知道吗

谢谢


Tags: 文件httpscomurl网页get链接example