出问题的代码部分是这样的,
![在这里插入图片描述](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