[英]Parse JSON response from api and filter results
大家好,我對這個領域很陌生,很抱歉我試圖制作一個 function 發送 http 發布請求>>獲取 json 響應並保存到變量以僅使用它。
我嘗試做這樣的事情:
def getexpiresdate():
lastdiclist = getSettingsFromFile()
WhoApi = lastdiclist['wxakey']
url = f'https://www.whoisxmlapi.com/whoisserver/WhoisService?'
apiurl = f'{url}apiKey={WhoApi}&domainName={askfordomain()}'
r = requests.get(apiurl).json()
results = r.json()
print(results)
getexpiresdate()
我得到了一些錯誤:
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
如果有人能幫助我理解我如何解析 json 並只提取一個值,我會很高興。
首先,檢查 URL 是否返回正確的 JSON。 其次,您要兩次轉換為 JSON。 要么使用:
r = requests.get(apiurl)
results = r.json()
要么
results = requests.get(apiurl).json()
您在請求響應中調用了 JSON function 兩次。 除此之外,如果您使用的是IPinfo Whois API 服務,您可以先試用curl
服務。
例如,命令將是 -
curl ipinfo.io/whois/net/comcast.net?token=df926f2066a12e
要使用 Whois API 使用 Python -
import requests
token = "XXXXXX" # get it from the dashboard
domain_name = "XXXXX"
url = f"https://ipinfo.io/whois/net/{domain_name}?token={token}"
response = requests.get(url=url)
if response.status_code == 200:
results = response.json()
else:
# make sure you have the access to the Whois API first. If not you will get a 401 erro
print(response.status_code)
print(response.json())
我認為您的 json 無效。 根據發布的代碼,您調用了兩次 json()。 所以你的正確代碼應該是這樣的:
import requests
response = requests.get(API_URL)
print(response.json())
您可以使用 IP2WHOIS 獲取 WHOIS 結果。 例如,
import requests
response = requests.get('https://api.ip2whois.com/v2?key=YOUR_API_KEY&domain=domain_name&format=json')
print(response.json())
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.