【问题标题】:Store the contents of a curl command which is executed from within a python script存储从 python 脚本中执行的 curl 命令的内容
【发布时间】:2018-05-27 13:08:17
【问题描述】:

当我执行这个[说文件名为 curl.py 并由 python curl.py 执行]

import subprocess
import json
subprocess.call([
        'curl',
        '--digest',
        '--user',
        'user:pass',
        'https://url'],
        )

在我的例子中,输出是一个 JSON 文件。 我在终端上看到了输出,但我希望它存储在同一个 python 文件中的一个对象中。

我怎样才能做到这一点?

【问题讨论】:

  • 是否需要使用curl?有 Python 库可以做同样的事情。
  • 是的,如果您想到任何返回相同的替代方案,请提出建议!

标签: python json curl


【解决方案1】:

根据 cmets,这里有一个没有 curl 的替代方案。我在这里使用requests (pip install requests):

import requests
url = 'http://httpbin.org/digest-auth/auth/user/pass'
r = requests.get(url, auth=requests.auth.HTTPDigestAuth('user', 'pass'))
print(r.json())  # no need to unpack JSON manually!

【讨论】:

  • 请点赞我的问题,让其他人看到这篇文章并得到他们的答案!
最近更新 更多