Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the URL of a redirect with Python

In Python, I'm using urllib2 to open a url. This url redirects to another url, which redirects to yet another url.

I wish to print out the url after each redirect.

For example

-> = redirects to

A -> B -> C -> D

I want to print the URL of B, C and D (A is already known because it's the start URL).

like image 875
Matthew H Avatar asked Feb 04 '11 20:02

Matthew H


People also ask

What is redirect URL example?

A redirect is when a web page is visited at a certain URL, it changes to a different URL. For instance, a person visits “website.com/page-a” in their browser and they are redirected to “website.com/page-b” instead.


1 Answers

You can easily get D by just asking for the current URL.

req = urllib2.Request(starturl, datagen, headers) res = urllib2.urlopen(req) finalurl = res.geturl() 

To deal with the intermediate redirects you'll probably need to build your own opener, using HTTPRedirectHandler that records the redirects.

like image 166
chmullig Avatar answered Sep 22 '22 02:09

chmullig