首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >python请求与JSONDecodeError随机中断

python请求与JSONDecodeError随机中断
EN

Stack Overflow用户
提问于 2018-09-24 22:22:05
回答 2查看 33.6K关注 0票数 14

我已经调试了几个小时了,为什么我的代码会随机中断这个错误:JSONDecodeError: Expecting value: line 1 column 1 (char 0)

这是我的代码:

代码语言:javascript
复制
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进行调试,并且遇到了以下“特殊”情况:

  1. requests.get(url)返回一个成功的200个响应,requests.get(url).text返回html。我在网上读到,当使用requests.get(url).json()时,这应该会失败,因为它将无法读取html,但不知怎么它不会中断。为什么会这样呢?
  2. requests.get(url)返回一个成功的200个响应,requests.get(url).text采用json格式。我不明白为什么当它到达requests.get(url).json()行时,它会与JSONDecodeError中断?

对于第2种情况,requests.get(url).text的确切值是:

代码语言:javascript
复制
{
  "submission": {
    "columns": [
      "pk",
      "form",
      "date",
      "ip"
    ],
    "records": [
      [
        "21197",
        "mistico-form-contacto-form",
        "2018-09-21 09:04:41",
        "186.179.71.106"
      ]
    ]
  }
}
EN

回答 2

Stack Overflow用户

发布于 2018-09-25 00:54:40

从这个API的文档来看,似乎只有JSON格式的响应,所以接收是很奇怪的。要增加接收JSON响应的可能性,可以将'Accept‘头设置为'application/json’。

我多次尝试使用参数查询这个API,但没有遇到JSONDecodeError。此错误可能是服务器端另一个错误的结果。为了处理它,except是一个json.decoder.JSONDecodeError,除了当前的ConnectionError错误(您当前的except ),并以与ConnectionError相同的方式处理这个错误。

下面是一个考虑到所有这些的例子:

代码语言:javascript
复制
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循环,因为我认为它在语义上更清晰。此函数还会在使用指数退避再次尝试之前等待(每次失败后等待的时间是原来的两倍)。

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

代码语言:javascript
复制
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']

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

票数 10
EN

Stack Overflow用户

发布于 2020-10-30 07:28:14

尝尝这个。可能会有用的

代码语言:javascript
复制
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)
票数 -2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52488117

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档