Avoiding redirects with urllib

F

Fernando Rodriguez

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!
 
T

tdahsu

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...
 
T

tdahsu

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
 
F

Fernando Rodriguez

Hello (e-mail address removed),
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...


The problem is that I'm using a subclass of FancyOpener and it doesn't raise
those exceptions.
 
F

Fernando Rodriguez

Hello Fernando,
The problem is that I'm using a subclass of FancyOpener and it doesn't
raise those exceptions.

Ok, forget it, I should have read the "fine" manual. O:)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top