【发布时间】:2014-04-26 21:46:07
【问题描述】:
我在循环中有一个循环,我正在使用 try n catch 一旦出现错误 try n catch 工作正常,但循环继续到下一个值。我需要的是循环中断从相同的值开始的地方不要继续到下一个,所以我如何用我的代码做到这一点[就像在其他语言中一样:在 c++ 中,它是 i--]
for
r = urllib2.urlopen(url)
encoding = r.info().getparam('charset')
html = r.read()
c = td.find('a')['href']
urls = []
urls.append(c)
#collecting urls from first page then from those url collecting further info in below loop
for abc in urls:
try:
r = urllib2.urlopen(abc)
encoding = r.info().getparam('charset')
html = r.read()
except Exception as e:
last_error = e
time.sleep(retry_timeout) #here is the problem once get error then switch from next value
我需要一种更 Pythonic 的方式来做到这一点。 等待答复。谢谢。
【问题讨论】:
-
欢迎来到 SO!请仅在已知问题非常特定于版本的情况下使用版本号:meta.stackexchange.com/questions/85358/…
-
请缩进您的代码,以便它是有效的 Python。请告诉我们更多关于
try和catch内部发生的事情。 -
另外,如果你写了整个句子,包括标点符号,那就太好了。拼写是一种奖励。
-
如果您有
for循环,请将其更改为while循环,并且仅在try块中增加i值i += 1。另外,我假设您的缩进在实际代码中是正确的。 -
现在看看上面我在做什么。谢谢
标签: python list python-2.7 web web-scraping