Avoiding redirects with urllib

tdahsu at gmail.com tdahsu at gmail.com
Sat Jun 14 18:29:26 EDT 2008


On Jun 14, 6:18 pm, tda... at gmail.com wrote:
> On Jun 14, 5:22 pm, Fernando Rodriguez
>
> <fernandoREMOVE_T... at easyjob.net> wrote:
> > Hi,
>
> > I'musing urllib to download pages from a site. How can I detect if a given
> > url is being redirected somewhere else? I want to avoid this, is it possible?
>
> > Thanks in advance!
>
> Try this:
>
> import urllib
> url_opener = urllib.URLopener() # create URLopener
> #You could also work with urllib.FancyURLopener
>
> try:
>     data = url_opener.open("http://www.somedomain.com/index.html")   #
> open index.html
> except IOError, error_code:
>     if error_code[0] == "http error":
>         if error_code[1] == 301:
>             #do something here
>         if error_code[2] == 302:
>             #do something here
>
> I hope that's of some help!  I think you may want to delve deeper into
> FancyURLopener...

That last part might better be:

if error_code[1] == 301:
    #do something here
elif error_code[1] == 302:
    #do something here



More information about the Python-list mailing list