python 如何使用requests下载文件
作者:阿宅gogo 时间:2023-06-08 20:36:58
使用requests下载文件
1、获取token,或者session
如不需要可忽略
login_url = "http://xxxx/api/auth/login"
login_data = {"username":"test3","password":"123456"}
login_res = requests.post(url=login_url,data = login_data)
token = login_res.json()["data"]["token"]
2、获取下载路径
如果请求后直接返回文件内容,可直接进行第三步
batch_url = "http://xxxx/api/models/batch"
batch_data = {"ids":"[4]","version_number":"[309]"}
headers = {"Authorization":"bearer %s" % token}
batch_res = requests.get(url=batch_url,params=batch_data,headers=headers)
3、根据下载路径拼接下载url
完成文件下载以及写入
file_path = batch_res.json()['data']['file_path']
file_name = batch_res.json()['data']['file_name']
down_url = "http://xxxx/api/report/down"
down_data = {"type":2,
"file_path":file_path,
"file_name":file_name,
"token":token
}
down_res = requests.get(url=down_url,params=down_data)
with open(file_name,"wb") as code:
code.write(down_res.content)
备注:
第二步返回json数据,包含路径、文件名,实际是文件生成过程,第三步下载在服务端生成的文件,有时第三步无法在页面F12查看到,需要使用抓包工具获取
用requests.get下载文件
不知道大家有没有遇到这样的问题
就是url源不稳定,时不时下载到空文件,我终于想到了一个解决的好办法,分享给大家。
def downloadfile(url,filename=None):
if(not filename):#如果参数没有指定文件名
filename=os.path.basename(url)#取用url的尾巴为文件名
leng=1
while(leng==1):
torrent=requests.get(url,headers=headers)
leng=len(list(torrent.iter_content(1024))) #下载区块数
if(leng==1):#如果是1 就是空文件 重新下载
print(filename,'下载失败,重新下载')
sleep(1)
else:
print(path,'下载完成')
with open(filename,'wb') as f:
for chunk in torrent.iter_content(1024):#防止文件过大,以1024为单位一段段写入
f.write(chunk)
来源:https://www.cnblogs.com/wbw-test/p/11984382.html
标签:python,requests,下载文件
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
js友好的表单验证程序vform
2007-08-16 13:32:00
vue 项目打包通过命令修改 vue-router 模式 修改 API 接口前缀
2024-05-05 09:08:16
pandas去除重复列的实现方法
2022-06-27 12:37:31
![](https://img.aspxhome.com/file/2023/9/91859_0s.png)
python自动化测试selenium操作checkbox和radiobox技术
2021-11-01 16:12:54
![](https://img.aspxhome.com/file/2023/3/107983_0s.png)
浅谈怎么给Python添加类型标注
2023-11-21 05:16:17
![](https://img.aspxhome.com/file/2023/1/124091_0s.png)
Python保姆式手把手带你掌握异常的捕获和处理
2022-04-30 14:10:18
![](https://img.aspxhome.com/file/2023/0/71480_0s.png)
keras自定义回调函数查看训练的loss和accuracy方式
2021-01-11 18:13:09
![](https://img.aspxhome.com/file/2023/4/128324_0s.jpg)
解决前后端分离 vue+springboot 跨域 session+cookie失效问题
2024-06-07 15:22:43
![](https://img.aspxhome.com/file/2023/3/123013_0s.png)
python使用selenium打开chrome浏览器时带用户登录信息实现过程详解
2023-07-19 05:00:37
![](https://img.aspxhome.com/file/2023/8/60298_0s.jpg)
python中的selenium安装的步骤(浏览器自动化测试框架)
2022-11-13 03:18:53
![](https://img.aspxhome.com/file/2023/9/125019_0s.jpg)
Python enumerate遍历数组示例应用
2023-06-10 16:59:26
![](https://img.aspxhome.com/file/2023/1/127141_0s.gif)
python 列表推导和生成器表达式的使用
2021-03-16 11:21:39
![](https://img.aspxhome.com/file/2023/2/118052_0s.png)
Tensorflow中的降维函数tf.reduce_*使用总结
2021-10-18 15:16:14
python操作openpyxl导出Excel 设置单元格格式及合并处理代码实例
2022-09-27 13:07:54
SQL Server在T-SQL语句中使用变量
2024-01-21 10:21:34
python解析xml文件操作实例
2022-01-02 10:39:13
GDAL 矢量属性数据修改方式(python)
2021-01-30 20:53:28
![](https://img.aspxhome.com/file/2023/7/78537_0s.jpg)
Django ValuesQuerySet转json方式
2021-12-05 07:15:34
python 将numpy维度不同的数组相加相乘操作
2022-03-26 06:14:06
![](https://img.aspxhome.com/file/2023/5/93425_0s.jpg)
事件检测
2009-04-11 18:03:00