Python While循环限制时间
发生错误时如何重试此函数,最多重试三次Python While循环限制时间,python,Python,发生错误时如何重试此函数,最多重试三次 def crawl(url): while True: try: data = os.popen('wget -qO- -T10 --tries=1 --header="Accept: text/html" --user-agent="User-Agent: Mozilla/5.0 (Windows NT 6.0) App
def crawl(url):
while True:
try:
data = os.popen('wget -qO- -T10 --tries=1 --header="Accept: text/html" --user-agent="User-Agent: Mozilla/5.0 (Windows NT 6.0) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.97 Safari/537.11 " %s'% url).read()
return data
except Exception, e:
print e
pass
crawl("http://aaa.com")
如何在此
循环中重试3次?您可以在循环条件中添加计数器
c = 0
while c < 3:
try:
..
except ...
c = c + 1
c=0
c<3时:
尝试:
..
除了
c=c+1
很好的解决方案。次要反馈点:expect
应该是除了:)当然:)已修复