【问题标题】:Parse JSON response from api and filter results解析来自 api 的 JSON 响应并过滤结果
【发布时间】:2021-07-26 10:23:06
【问题描述】:

大家好,我是这个领域的新手,很抱歉我的无知 我试图制作一个发送http post请求的函数>>获取json响应>>只取'expiresdate'值并保存到变量中以供以后使用。

我尝试做这样的事情:

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 并只提取一个值,我会很高兴。

【问题讨论】:

    标签: python python-3.x python-requests


    【解决方案1】:

    首先,检查 URL 是否返回正确的 JSON。 其次,您要转换为 JSON 两次。要么使用:

    r = requests.get(apiurl)
    results = r.json()
    

    results = requests.get(apiurl).json()
    

    【讨论】:

    • 我在尝试此操作时出错...从无 json.decoder.JSONDecodeError 引发 JSONDecodeError("Expecting value", s, err.value): Expecting value: line 1 column 1 (char 0)
    • 使用 postman 或 curl 发送请求到https://www.whoisxmlapi.com/whoisserver/WhoisService?apiKey=XXXXX&domainName=YYYY 你得到什么结果?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-14
    • 1970-01-01
    • 1970-01-01
    • 2018-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多