【发布时间】:2017-04-11 23:01:59
【问题描述】:
我正在运行一个从 API url 获取 json 数据的代码,场景是我正在尝试自定义异常,而未获取 URL 响应(有时响应显示 200 仍然它不获取数据)在此如果代码应该从头开始重新执行。
代码:
import json
import urllib
url = 'www.google.com'
status = url.getcode()
if(status != 200):
# re-execute the code
data = json.load(urllib.urlopen(url))
if (data == null):
#re-execute the code
通过互联网搜索时找不到合适的解决方案
有人可以帮忙吗?
【问题讨论】:
-
使用循环怎么样?另外
null在Python中不存在,你可以使用data is None或者if not data来验证。 -
什么是
null?
标签: python python-2.7 api networking