python - 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)
我怎样才能摆脱这个错误。
解决方案
推荐阅读
- jenkins - 调用块传递给带有参数的管道步骤,从插件
- javascript - 如何从我的控制器向我的 ajax 方法发送数据?
- node.js - SyntaxError:请求的模块“graphql-relay”未提供名为“fromGlobalId”的导出
- java - 返回字符串中元素的真值的布尔语句直接相互跟随
- javascript - 有没有办法在 React 中进行嵌套导入?
- javascript - 替换对象中的嵌套字段值
- node.js - SSH 隧道阻止我生成的脚本退出
- html - 在 Bootstrap 4 网格区域内显示视频
- openshift - OpenShift 中所有 SCC 的详细描述
- c# - Array[i, j] = rnd.Next(0, 100); 的解释