【发布时间】:2018-06-15 08:08:41
【问题描述】:
目前,我正在尝试将 CURL 请求转换为 Python 脚本。
curl $(curl -u username:password -s https://api.example.com/v1.1/reports/11111?fields=download | jq ".report.download" -r) > "C:\sample.zip"
我试过pycurl,由于知识有限,没有成功。
作为一种解决方案,我发现可以通过 python 运行命令。 https://www.raspberrypi.org/forums/viewtopic.php?t=112351
import os
os.system("curl -K.........")
和其他解决方案(基于搜索更常见)使用子进程
import subprocess
subprocess.call(['command', 'argument'])
目前,我不确定要搬到哪里以及如何使此解决方案适应我的位置。
import os
os.system("curl $(curl -u username:password -s https://api.example.com/v1.1/reports/11111?fields=download | jq '.report.download' -r) > 'C:\sample.zip'")
'curl' is not recognized as an internal or external command,
operable program or batch file.
255
附注- 更新 v1
有什么建议吗?
import requests
response = requests.get('https://api.example.com/v1.1/reports/11111?fields=download | jq ".report.download" -r', auth=('username', 'password'))
本作品没有“| jq ".report.download”这部分,但这是主要部分,最后只给出了下载文件的链接。
有什么办法吗?
【问题讨论】:
-
requests模块应该可以帮助你 -
curl可执行文件不在PATH环境变量中,尝试将curl添加到路径或使用apt从apt安装它 -
@Rakesh 已更新(请查看)
-
@sriharsha_bhat 有潜力,但当我尝试使用 JQ 时出现问题。
-
@Rakesh 看,请求确实有效,但管道后的 jq 部分不起作用,这给了我而不是完整的 JSON,只是它的内容,有什么建议吗???
标签: python curl subprocess pycurl