我正在使用第三方服务填充本地数据库。我有一个网址列表(大约 500 个)。我在循环中调用每个网址,并使用返回的数据更新我的数据库。代码流程如下所示:
for url in urllist:
req = urllib.urlopen(url)
data = json.loads(req.read())
req.close()
#update the db using data here
每当我运行这段代码时,脚本都会在随机点失败,并显示错误消息“名称或服务未知”。这与 url 没有任何关系,因为脚本在随机点失败(即一次运行中的第 50 次迭代,以及另一次运行中的第 60 次迭代)
这可能是什么原因?
如果您使用了错误的代理或存在网络问题,您可以尝试以下操作:
for url in urllist:
retry = 0
while True: # retry request
try:
req = urllib.urlopen(url)
resp_data = req.read() # in call read() network still processing
except Exception as e: # TODO need more detailed handling
if retry > 3: # 3 this is serious problem. exit
raise e
retry += 1 # retry
else:
data = json.loads()
req.close() # not needed
break
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)