Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何下载通过http请求提供给我下载的文件?_Python_Python 3.x_Python Requests - Fatal编程技术网

Python 如何下载通过http请求提供给我下载的文件?

Python 如何下载通过http请求提供给我下载的文件?,python,python-3.x,python-requests,Python,Python 3.x,Python Requests,我正在尝试下载file.io上存储的文件,但问题是我得到了一个2kb的文件。我怎样才能下载它? 在浏览器中打开链接时,我会看到下载窗口。这是我正在使用的代码 url = "https://www.file.io/" r = requests.get(url, allow_redirects=True) filename = "filedownloaded" open(filename, 'wb').write(r.content) io有一个非常易于使用的cURL api 您需要知道目标文件扩

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

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

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

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

curl-o test.pnghttps://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])

下载是通过javascript启动的吗?你能为cURL提供一个API吗?还有,为什么要使用os.system而不是subprocess模块,后者是文档为前者推荐的模块?@AMC我编辑了我的帖子,以包含subprocess模块。与操作系统相比,这有什么好处?您可以找到有关此主题的一些信息和: