请求Python3从WP站点检索excel文件的问题



这是我的问题,如果我键入,我正试图从WP网站下载excel xlsx文件

我直接在浏览器中为代码中一个名为stock的变量分配的url,Firefox完美地下载了它。

我正试图用Python来实现这一点,所以我制作了一个使用请求和Pandas进行处理和操作的脚本。

然而,尽管该文件似乎是下载的,但它返回了一个错误,我尝试使用open和with open,正如我在这里发现的类似问题所建议的那样,但在我的情况下,它返回了错误"ValueError:Seek of closed file",我尝试了对代码的几种变体,但没有结果,结果总是错误。

这是我的代码

import pandas as pd
import requests, os
import http.client
http.client.HTTPConnection._http_vsn = 10
http.client.HTTPConnection._http_vsn_str = 'HTTP/1.0'
# Url of the same link I used to manually fetch the file
stock = 'https://filmar.com/wp-content/uploads/2021/05/Apple-Lot-5-14-21.xlsx'
resp = requests.get(stock)  # passed the GET method to the http request with the URL
print("Downloading...") # This works
# When I try to retrieve the file it fails 
with open('Apple-Lot-5-14-21.xlsx', 'wb') as output:
output.write(resp.content)

print('The file has been downloaded') # this is printed
# The error happens when I try to assign the file to the pd.read_excel method in Pandas
apple = pd.read_excel(output)

附录

在输入@MattDMo提供的代码resp-object后,显然存在权限问题或其他问题,因为在分析响应对象models.response时,它返回了一个404,但没有找到,所以这要么是保护,要么是服务器上发生的重定向,所以请求检索到一个空文件。

不能将output传递给pd.read_excel(),因为当with上下文管理器退出时,对文件(output(的引用将被销毁。这里的一个选项是,如果真的不需要为其他任何事情保存Excel文件,那么将resp.content直接传递给read_excel()。或者,如果您想将Excel文件用于备份或其他目的,请创建一个文件名变量,如下所示:

xl_file = 'Apple-Lot-5-14-21.xlsx'

然后在调用with open(...和调用read_excel()时都使用该变量,因为该函数可以同时使用文件名和类文件对象。

另外要注意的是,我不确定您为什么使用http.client,因为据我所知,requests没有考虑任何这些值。

最新更新