如何在发生错误时重试此功能,最多重试三次?
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")
如何在此while
循环中重试3次?
答案 0 :(得分:1)
你可以在循环条件中添加一个计数器。
c = 0
while c < 3:
try:
..
except ...
c = c + 1