473,387 Members | 1,745 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Avoiding redirects with urllib


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!
Jun 27 '08 #1
4 2442
On Jun 14, 5:22*pm, Fernando Rodriguez
<fernandoREMOVE_T...@easyjob.netwrote:
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...
Jun 27 '08 #2
On Jun 14, 6:18*pm, tda...@gmail.com wrote:
On Jun 14, 5:22*pm, Fernando Rodriguez

<fernandoREMOVE_T...@easyjob.netwrote:
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
Jun 27 '08 #3
Hello td****@gmail.com,
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.
Jun 27 '08 #4
Hello Fernando,
>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.
Ok, forget it, I should have read the "fine" manual. O:-)
Jun 27 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Richard Shea | last post by:
Hi - I'm new to Python. I've been trying to use URLLIB and the 'tidy' function (part of the mx.tidy package). There's one thing I'm having real difficulties understanding. When I did this ... ...
11
by: Pater Maximus | last post by:
I am trying to implement the recipe listed at http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/211886 However, I can not get to first base. When I try to run import urllib...
0
by: Shane Hathaway | last post by:
I started experimenting with SOAPpy yesterday and immediately hit a snag. Both web services I tried simply hung and never replied. After a lot of digging, I found out what was going wrong:...
0
by: Pieter Edelman | last post by:
Hi all, I'm trying to submit some data using a POST request to a HTTP server with BASIC authentication with python, but I can't get it to work. Since it's driving me completely nuts, so here's...
1
by: Timothy Wu | last post by:
Hi, I'm trying to fill the form on page http://www.cbs.dtu.dk/services/TMHMM/ using urllib. There are two peculiarities. First of all, I am filling in incorrect key/value pairs in the...
4
by: william | last post by:
I've got a strange problem on windows (not very familiar with that OS). I can ping a host, but cannot get it via urllib (see here under). I can even telnet the host on port 80. Thus network...
6
by: justsee | last post by:
Hi, I'm using Python 2.3 on Windows for the first time, and am doing something wrong in using urllib to retrieve images from urls embedded in a csv file. If I explicitly specify a url and image...
5
by: supercooper | last post by:
I am downloading images using the script below. Sometimes it will go for 10 mins, sometimes 2 hours before timing out with the following error: Traceback (most recent call last): File...
5
by: chrispoliquin | last post by:
Hi, I have a small Python script to fetch some pages from the internet. There are a lot of pages and I am looping through them and then downloading the page using urlretrieve() in the urllib...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.