How to get the URL of a redirect with Python

Question:

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

Asked By: Matthew H

||

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.

Answered By: chmullig

Probably the best way is to subclass urllib2.HTTPRedirectHandler. Dive Into Python’s chapter on redirects may be helpful.

Answered By: Wooble

For Python 3, the solution with urllib is much simpler:

import urllib


def resolve(url):
    return urllib.request.urlopen(url).geturl()
Answered By: jadelord
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.