python - 如何下载通过 http 请求提供给我下载的文件?
问题描述
我正在尝试在 file.io 上下载存储的文件,但问题是我得到了一个 2kb 的文件。我怎样才能下载它?在浏览器中打开链接时,我得到下载窗口。这是我正在使用的代码。
url = "https://www.file.io/"
r = requests.get(url, allow_redirects=True)
filename = "filedownloaded"
open(filename, 'wb').write(r.content)
解决方案
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])
推荐阅读
- c++ - 操作系统如何检测 C 中的超出范围的分段错误?
- python - Python ATM 程序未按预期工作
- java - 在我的 Mac 上使用多个版本的 Java
- google-apps-script - 如何使用 Google Web App 显示自定义网页?
- html - 使用 Xampp 自动生成的 Div
- android - 从 Kotlin (Android) 中的监听器 onSuccess() 返回一个值
- image - 为什么将网站部署到 Vercel 时图像大小不同?
- javascript - Js用相同的按钮翻转/取消翻转
- php - 如何在我的多维关联数组中回显正确的键?
- angular - 如何在 Angular 材料中为 Mat Date Range Picker 设置值