如何使用Python的请求包访问API Get的特定元素?

2024-09-24 00:30:17 发布

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

嗨,我目前正在使用Python3.X运行以下代码:

import requests

jurisdiction = 'us'

name = 'netflix'

limit = 1


r = requests.get('https://opencorporates.com/reconcile?query={%22query%22:%22' + name + '%22,%20%22limit%22:' + str(limit) + ',%20%22jurisdiction_code%22:%22' + jurisdiction + '%22}')

print(type(r))
print(r.text)

它的输出是

<class 'requests.models.Response'>

{"result":[{"id":"/companies/gb/12022722","name":"AMAZON-UK LIMITED","type":[{"id":"/organization/organization","name":"Organization"}],"score":69.0,"match":false,"uri":"https://opencorporates.com/companies/gb/12022722"}],"duration":157.957621}

我希望能够从响应中访问公司名称,然后将其添加到列表中。因此,我可以迭代一堆名称/辖区,并在最后有一个列表,我可以导出到csv(或任何东西)。你知道吗

我认为使用到.json或者json.dump文件或者类似的东西可能有用,但我不知道具体怎么做?如果需要的话,我愿意进口更多的包装,如熊猫等。你知道吗


Tags: namehttps名称comid列表typerequests
2条回答

你可以试试这个。你知道吗

data = {"result":[{"id":"/companies/gb/12022722","name":"AMAZON-UK LIMITED","type":[{"id":"/organization/organization","name":"Organization"}],"score":69.0,"match":False,"uri":"https://opencorporates.com/companies/gb/12022722"}],"duration":157.957621}


for i in data['result']:
    print(i['name'])

导入json,您可以读取公司名称,如本文件:你知道吗

import json

data = json.loads(r.text)

#initialize your list 
namesList = []

for s in data['result']:
    name = s['name']
    namesList.append(name)

相关问题 更多 >