A
A
Andrew2014-02-04 19:59:52
Python
Andrew, 2014-02-04 19:59:52

Urllib2 in python: unable to get page, how to go to next page in this case?

I get the page in python like this:

for x in range(1, 10):
   response = urllib2.urlopen("http://site.com/id="+str(x))
   page = response.read()

Sometimes there are such problems that the page cannot be retrieved. How can I go to the next page in this case?
That is, let's say we are trying to get the site.com/id=3 page, but it does not respond. How do I navigate to site.com/id=4?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
Konstantin Dovnar, 2014-02-04
@SolidlSnake

If it throws an exception - catch it with try\except , then the program will not be interrupted.

V
Valery, 2014-02-04
@Vintorez

You have 2 options:
1. Before sending a get request, send a head request in order to check what code will be returned (in this case, no exception is thrown).
2. Catch the get request dispatch exception as @SolidlSnake said with try/except blocks.
Pretty clear examples of both of these options are given here:
stackoverflow.com/a/16778473

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question