CURL 请求 Python 作为命令行
CURL request to Python as command line
目前,我正在尝试将 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.........")
和其他解决方案(基于搜索更常见)使用 subprocess
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
P.S。 - 更新 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
”这部分,但这是主要部分,最后只给出 link 下载文件。
有什么解决办法吗?
您可以使用此站点将命令的实际 curl 部分转换为适用于请求的内容:https://curl.trillworks.com/
从那里开始,只需使用请求对象的 .json()
方法来执行您需要执行的任何处理。
终于可以这样保存了:
import json
with open('C:\sample.zip', 'r') as f:
json.dump(data, f)
错误'curl' is not recognized as an internal or external command
表示python找不到安装curl的位置。如果您已经安装了 curl,请尝试提供安装 curl
的完整路径。例如,如果 curl.exe 位于 C:\System32,try
import os
os.system("C:\System32\curl $(curl -u username:password -s https://api.example.com/v1.1/reports/11111?fields=download | jq '.report.download' -r) > 'C:\sample.zip'")
但这绝对不是 pythonic 做事的方式。相反,我建议使用 requests
模块。
为此需要调用requests模块两次,首先从https://api.example.com/v1.1/reports/11111?fields=download
下载json内容,获取report.download
指向的新url,然后再次调用requests下载数据来自新 url。
按照这些思路应该可以让您前进
import requests
url = 'https://api.example.com/v1.1/reports/11111'
response = requests.get(url, params=(('fields', 'download'),),
auth=('username', 'password'))
report_url = response.json()['report']['download']
data = requests.get(report_url).content
with open('C:\sample.zip', 'w') as f:
f.write(data)
目前,我正在尝试将 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.........")
和其他解决方案(基于搜索更常见)使用 subprocess
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
P.S。 - 更新 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
”这部分,但这是主要部分,最后只给出 link 下载文件。
有什么解决办法吗?
您可以使用此站点将命令的实际 curl 部分转换为适用于请求的内容:https://curl.trillworks.com/
从那里开始,只需使用请求对象的 .json()
方法来执行您需要执行的任何处理。
终于可以这样保存了:
import json
with open('C:\sample.zip', 'r') as f:
json.dump(data, f)
错误'curl' is not recognized as an internal or external command
表示python找不到安装curl的位置。如果您已经安装了 curl,请尝试提供安装 curl
的完整路径。例如,如果 curl.exe 位于 C:\System32,try
import os
os.system("C:\System32\curl $(curl -u username:password -s https://api.example.com/v1.1/reports/11111?fields=download | jq '.report.download' -r) > 'C:\sample.zip'")
但这绝对不是 pythonic 做事的方式。相反,我建议使用 requests
模块。
为此需要调用requests模块两次,首先从https://api.example.com/v1.1/reports/11111?fields=download
下载json内容,获取report.download
指向的新url,然后再次调用requests下载数据来自新 url。
按照这些思路应该可以让您前进
import requests
url = 'https://api.example.com/v1.1/reports/11111'
response = requests.get(url, params=(('fields', 'download'),),
auth=('username', 'password'))
report_url = response.json()['report']['download']
data = requests.get(report_url).content
with open('C:\sample.zip', 'w') as f:
f.write(data)