【发布时间】:2021-05-17 18:45:43
【问题描述】:
这是我的问题,如果我键入,我正在尝试从 WP 网站下载 excel xlsx 文件
我在我的代码中分配给一个变量的网址,称为股票,直接在浏览器中,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,未找到,所以要么是保护,要么是重定向这发生在服务器上,因此 requests 检索一个空文件。
【问题讨论】:
-
您已将文件下载到内存中,但尚未将其保存到磁盘。
标签: python pandas python-requests