Python urllib2 multiple try statement on urlopen()

Posted by Kura on Stack Overflow See other posts from Stack Overflow or by Kura
Published on 2010-05-26T21:59:59Z Indexed on 2010/05/26 22:21 UTC
Read the original article Hit count: 263

Filed under:
|

So, simply I want to be able to run a for across a list of URLs, if one fails then I want to continue on to try the next.

I've tried using the following but sadly it throws and exception if the first URL doesn't work.

servers = ('http://www.google.com', 'http://www.stackoverflow.com')
for server in servers:
    try:
        u = urllib2.urlopen(server)
    except urllib2.URLError:
        continue
    else:
        break
else:
    raise

Any ideas?

Thanks in advance.

© Stack Overflow or respective owner

Related posts about python

Related posts about urllib2