python - Python 3 中带有选项的 curl 命令
问题描述
在requests
库中,如果需要执行
curl http://www.example.com/file.xlsx
命令是
response = requests.get('http://www.example.com/file.xlsx')
如果我想使用-O
选项执行命令怎么办?
curl http://www.example.com/file.xlsx -O
我该如何做到这一点?
解决方案
没有明确的“-O”=写入相同的文件名。如果需要,可以将 url 存储在变量中并使用多种获取方式。一种懒惰的方式是rpartition('/')[2]
在 url 字符串上使用。
其余快速保存结果的代码在这里:
import requests
from pathlib import Path
response = requests.get('http://www.example.com/file.xlsx')
Path('file.xlsx').write_bytes(response.content)
# or if you want to have the file name extracted use this less readable line
# Path(response.url.rpartition('/')[2]).write_bytes(response.content)
推荐阅读
- ruby - 如何使用传递给 Graphql ruby 字段的参数来转换结果?
- javascript - 如何在 amChart 中更改气球位置
- phaser-framework - 移动设备上的活动指针问题,使用 Phaser 3
- javascript - 将数据推送到对象/数组以供以后比较
- plone - 页面刷新时日期消失(NewsItem)
- message-queue - Cloud Pub/Sub 订阅者崩溃故障转移场景
- jira - 连接 CA 敏捷集会与融合汇合
- python - 从 python 包中正确导入以满足内部包的需求
- kubernetes - Kubernetes Let's Encrypt cert-manager Error secret not found
- javascript - 使用流注释具有许多属性的类?