首页 > 解决方案 > json.decoder.JSONDecodeError 同时将响应转换为 json

问题描述

我正在执行一项任务,我必须从外部 API 获取 20,000 条记录的数据。为此,我在 python 中使用 requests 模块,下面是我的代码

def getdata():
    datanotfound=[]
    """ fetching the values from database to pass in the url  """
    values = mongo.db.collection.find()
    for value in values:
        value = pro.get("name")
        """ adding the value parameter into the url and fetching the data  """
        r = requests.get('myurl'+ value)
        if r!= None:
            response = r.json()
            info = response.get(val1)
            if info != None:
                val2 = info.get("val2")
                val3 = info.get("val3")
                val4 = info.get("val4")
                val5 = info.get("val5")
                """ saving the response in mongodb  """
                mongo.db.collection.insert_one({
                    'name':val1,
                    "address":val2,
                    "length":val3,
                    "width":val4,
                    "data":val5,           
                    })
            else:
                """ sending the values for which response was None  """
                datanotfound.append(value)
    return jsonify({'datanotfound':datanotfound})

这对于一些记录运行良好,但有时我得到以下错误:

raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 2 column 5 (char 5)

我怎样才能摆脱这个错误。

标签: pythonjsonpython-3.xpython-requests

解决方案


推荐阅读