错误json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)的解决记录

出问题的代码部分是这样的,
![在这里插入图片描述](https://img-blog.csdnimg.cn/20201019202700912.png#pic_center在这里插入图片描述

这是因为返回状态码为200,但是返回内容为空导致的。
我的判断条件是response.status_code == 200

import requests
import sys
struct = {}
#执行API调用并存储响应
url = 'http://api.gihub.com/search/repositories?q=language:python&sort=stars'
print("status code",requests.get(url).status_code)
#将API响应存储在一个变量中
try:
    response = requests.get(url,timout = 3)
    #response.status_code == 200 
    if response.content:
        response_dict = response.json()
        print("total repositories:",response_dict['total_count'])
        #探索有关仓库的信息
        repo_dicts = response_dict['items']
        print("repositories returned:",len(repo_dicts))
        #研究第一个仓库
        repo_dict = repo_dicts[0]
        print("\nkeys:",len(repo_dict))
        for key in sorted(repo_dict.keys()):
            print(key)
except:
    print("ok")

不要直接判断返回状态,因为返回为200的时候也可能内容为空,就会报错,判断返回内容是否None。httpcode200也可能返回内容None

参考:解决json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)问题
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值