首页 > 解决方案 > 如何下载通过 http 请求提供给我下载的文件?

问题描述

我正在尝试在 file.io 上下载存储的文件,但问题是我得到了一个 2kb 的文件。我怎样才能下载它?在浏览器中打开链接时,我得到下载窗口。这是我正在使用的代码。

url = "https://www.file.io/"
r = requests.get(url, allow_redirects=True)
filename = "filedownloaded"
open(filename, 'wb').write(r.content)

标签: pythonpython-3.xpython-requests

解决方案


file.io 有一个非常易于使用的 cURL api。

您需要知道目标文件扩展名和一次性 url。例如,如果我将 png 上传到 file.io,这将是我的 cURL 请求,并且该文件将下载到当前目录中。

curl -o test.png https://file.io/fileURL

由于您正在为此编写脚本,因此我假设您将拥有此信息。

import os
directory = "cd /target/directory/"
curlReq = "curl -o "
#you will have to retrive the info for these variables
filename = "filename.extension "
url = "https://file.io/uniqueURL"

os.system(directory)
os.system(curlReq + filename + url)

我可能还有其他方法,但这对我有用。

编辑:使用子进程的 cURL 请求。

from subprocess import call

url = "https://file.io/uniqueURL"
filename = "filename.extension"
call(["cd","/target/directory/"])
call(["curl","-o",filename,url])

推荐阅读