【问题标题】:Curl output json list卷曲输出json列表
【发布时间】:2021-01-22 20:06:38
【问题描述】:

我有一个 curl 突击队并把它拿回来

数据:

import os

cmd = "curl 'https://localhost/api?status=RUNNING'"
data = os.system(cmd)
print(str(data))

输出:

root@test /home/local/ # python3 main.py
[
  "testdir1",
  "testdir2",
  "testdir3"
]0
root@test /home/local/ #

如何将testdir1testdir2testdir3 放入变量中?

使用这段代码,我得到完全相同的输出:

cmd = "curl 'https://localhost/api?status=RUNNING'"
data = os.system(cmd)
for v in str(data):
  print(v)

我也不知道最后的 0 是从哪里来的。

【问题讨论】:

  • 0是curl子进程的返回码。不要使用os.system,直接使用urllib或者requests即可。
  • 你能告诉我脚本在 urllib 中的样子吗?
  • 不。这并不难。阅读文档docs.python.org/3/library/urllib.request.html 自己尝试一下,如果您有任何具体问题或问题,请回来。

标签: python python-3.x curl


【解决方案1】:

os.system 正在运行一个命令,该命令本身可以写入标准输出。
此外,您在此之后执行 print ('0')。

import os
rc = os.system('echo hello')
print('the command returned:', rc)

你好
返回的命令:0

您可以在此处找到有关 os.system 返回值的更多信息
https://docs.python.org/3/library/os.html#os.system

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-12
    • 2011-05-15
    • 2016-04-23
    • 2015-03-25
    • 1970-01-01
    • 2020-04-22
    相关资源
    最近更新 更多