在Python中提取部分JSON响应

2024-04-23 23:47:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我是一个编程初学者,我正在尝试使用一个API来自动化我的工作。在

我得到的响应很好,但我只对两个值感兴趣,即主机和端口。在

这是我的一部分代码

import requests

url = "https://proxy6.net/api/xxx"

def descr():
    return 88


querystring = {"descr":descr()}

headers = {
    'Cache-Control': "no-cache",
    'Postman-Token': "xxx"
    }

response = requests.request("GET", url, headers=headers, params=querystring)

print(response.text)

如何才能只打印主机和端口值?在

事先非常感谢


Tags: 端口代码httpsimportapiurlresponse编程
2条回答
import requests
import json

url = "https://proxy6.net/api/xxx"

def descr():
    return 88


querystring = {"descr":descr()}

headers = {
    'Cache-Control': "no-cache",
    'Postman-Token': "xxx"
    }

response = requests.request("GET", url, headers=headers, params=querystring)
response = response.json()
print(response['list']['xx']['host'])
print(response['list']['xx']['port'])

说明:

^{pr2}$

如果你有疑问,一定要告诉我

response = requests.request(...)
data = response.json()
for value in data['list'].values():
    host = value['host']
    port = value['port']

    print host, port  # OP uses Python 2

    break
else:  # we didn't break, which means data['list'] was empty
    raise RuntimeError('Empty list')

response2 = requests.request('POST', ...)  # you can use host and port here

相关问题 更多 >