python请求随机中断,JSONDecodeError

python请求随机中断,JSONDecodeError,python,json,python-requests,jsondecoder,Python,Json,Python Requests,Jsondecoder,我已经调试了好几个小时,为什么我的代码会随机出现以下错误:jsondecoderror:期望值:第1行第1列(char 0) 这是我的代码: while True: try: submissions = requests.get('http://reymisterio.net/data-dump/api.php/submission?filter[]=form,cs,'+client+'&filter[]=date,cs,'+since).json()['subm

我已经调试了好几个小时,为什么我的代码会随机出现以下错误:
jsondecoderror:期望值:第1行第1列(char 0)

这是我的代码:

while True:
    try:
        submissions = requests.get('http://reymisterio.net/data-dump/api.php/submission?filter[]=form,cs,'+client+'&filter[]=date,cs,'+since).json()['submission']['records']
        break
    except requests.exceptions.ConnectionError:
        time.sleep(100)
我一直在通过打印
requests.get(url)
requests.get(url).text进行调试,我遇到了以下“特殊”情况:

  • requests.get(url)
    返回一个成功的200响应,
    requests.get(url).text
    返回html。我在网上读到,当使用
    requests.get(url).json()
    时,这应该会失败,因为它将无法读取html,但不知何故它不会中断。为什么会这样

  • requests.get(url)
    返回一个成功的200响应,
    requests.get(url)。文本为json格式。我不明白为什么当它转到
    requests.get(url.json()
    行时,它会因为JSONDecodeError而中断

  • 案例2的
    requests.get(url).text
    的确切值为:

    {
      "submission": {
        "columns": [
          "pk",
          "form",
          "date",
          "ip"
        ],
        "records": [
          [
            "21197",
            "mistico-form-contacto-form",
            "2018-09-21 09:04:41",
            "186.179.71.106"
          ]
        ]
      }
    }
    
    看看这个API的响应,似乎只有JSON格式的响应,所以接收HTML是很奇怪的。要增加接收JSON响应的可能性,可以将“Accept”头设置为“application/JSON”

    我曾多次尝试使用参数查询此API,但没有遇到
    JSONDecodeError
    。此错误可能是服务器端的另一个错误导致的。要处理它,
    except
    a
    json.decoder.JSONDecodeError
    除了当前的
    ConnectionError
    错误之外,您还可以使用与
    ConnectionError
    相同的方法处理此错误

    下面是一个牢记所有这些的示例:

    import requests, json, time, random
    
    def get_submission_records(client, since, try_number=1):
        url = 'http://reymisterio.net/data-dump/api.php/submission?filter[]=form,cs,'+client+'&filter[]=date,cs,'+since
        headers = {'Accept': 'application/json'}
        try:
            response = requests.get(url, headers=headers).json()
        except (requests.exceptions.ConnectionError, json.decoder.JSONDecodeError):
            time.sleep(2**try_number + random.random()*0.01) #exponential backoff
            return get_submission_records(client, since, try_number=try_number+1)
        else:
            return response['submission']['records']
    
    我还将此逻辑包装在递归函数中,而不是使用
    while
    循环,因为我认为它在语义上更清晰。此函数还可以在使用指数退避重试之前等待(每次失败后等待的时间延长一倍)

    编辑:对于Python2.7,试图解析错误json的错误是
    ValueError
    ,而不是
    JSONDecodeError

    import requests, time, random
    
    def get_submission_records(client, since, try_number=1):
        url = 'http://reymisterio.net/data-dump/api.php/submission?filter[]=form,cs,'+client+'&filter[]=date,cs,'+since
        headers = {'Accept': 'application/json'}
        try:
            response = requests.get(url, headers=headers).json()
        except (requests.exceptions.ConnectionError, ValueError):
            time.sleep(2**try_number + random.random()*0.01) #exponential backoff
            return get_submission_records(client, since, try_number=try_number+1)
        else:
            return response['submission']['records']
    

    因此,只需将该
    行更改为包含
    ValueError
    ,而不是
    json.decoder.JSONDecodeError

    ,试试这个。这可能有用

    while True:
            try:
                submissions = requests.get('http://reymisterio.net/data-dump/api.php/submission?filter[]=form,cs,'+client+'&filter[]=date,cs,'+since).json()['submission']['records']
                sub = json.loads(submissions.text)
                print(sub)
                break
            except requests.exceptions.ConnectionError:
                time.sleep(100)
    

    当您尝试
    json.loads(requests.get(url.text)
    时,它是否有效?是的,当我接收html输出时,我非常困惑。谢谢这个例子看起来很棒,我会试试看。希望header参数能解决这个问题。我一直在测试这个实现,我发现这个错误:AttributeError:“module”对象没有属性“JSONDecodeError”。我正在使用Python2.7,是因为这个吗?@mateocam哦,是的,在Python2.7中,错误是
    ValueError
    ,我会更新我的答案,但基本上你应该
    除了
    a
    ValueError
    之外,而不是
    json.decoder.jsondecoderor
    谢谢!!这太有帮助了,非常感谢!!我使用了您的答案,但得到了以下错误:“AttributeError:'dict'对象没有属性'decoder'”。它应该是json.jsondecoderror而不是json.decoder.jsondecoderror吗?()至少你可以说你添加了
    sub=json.loads(submissions.text)
    行。@Willysatrionugroho这像是一个参与奖杯吗?