[英]Trying to parse JSON from API request with python
這是我的代碼
import requests
import json
api_get = requests.get('https://api.domainsdb.info/v1/domains/search?domain=dou.ua')
api_answer = api_get.json()
print(api_answer)
這是答案
{'domains': [{'domain': 'dou.ua', 'create_date': None, 'update_date': '2019-01-08T12:41:02.907006', 'country': 'GB', 'isDead': 'False', 'A': ['178.79.140.30'], 'NS': ['ns3.linode.com', 'ns1.linode.com', 'ns2.linode.com', 'ns5.linode.com', 'ns4.linode.com'], 'CNAME': None, 'MX': [{'exchange': 'alt1.aspmx.l.google.com', 'priority': 1}, {'exchange': 'alt2.aspmx.l.google.com', 'priority': 1}, {'exchange': 'aspmx.l.google.com', 'priority': 0}], 'TXT': ['v=spf1 include:servers.mcsv.net ?all']}], 'total': 1, 'time': '771', 'next_page': None}
現在我需要考慮“國家”的價值
import requests
import json
api_get = requests.get('https://api.domainsdb.info/v1/domains/search?domain=dou.ua')
api_answer = api_get.json()
country = json.loads(api_answer)
print(country["domains"]["country"])
我有錯誤
TypeError: the JSON object must be str, bytes or bytearray, not 'dict'
當我做
import requests
import json
api_get = requests.get('https://api.domainsdb.info/v1/domains/search?domain=dou.ua')
api_answer = api_get.json()
country = json.dumps(api_answer)
print(country["domains"]["country"])
我有錯誤
print(country["domains"]["country"])
TypeError: string indices must be integers
我不明白我必須做什么。
嘗試這個
import requests
import json
api_get = requests.get('https://api.domainsdb.info/v1/domains/search?domain=dou.ua')
api_answer = api_get.json()
print(api_answer['domains'][0]['domain'])
當您使用 json.dumps 時,返回的值是通過轉儲 json 形成的字符串。 您不能通過鍵對字符串進行索引,而是應該在您的情況下對 json ie api_answer
進行索引。
import requests
import json
api_get = requests.get('https://api.domainsdb.info/v1/domains/search?domain=dou.ua')
api_answer = api_get.json()
print(api_answer['domains'][0]['country'])
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.