Python Requests library redirect new url

I've been looking through the Python Requests documentation but I cannot see any functionality for what I am trying to achieve.

In my script I am setting allow_redirects=True.

I would like to know if the page has been redirected to something else, what is the new URL.

For example, if the start URL was:

And the final URL is

How do I get that URL?


You are looking for the request history.

The response.history attribute is a list of responses that led to the final URL, which can be found in response.url.

response = request.get(someurl)
if response.history:
    print "Request was redirected"
    for resp in response.history:
        print resp.status_code, resp.url
    print "Final destination:"
    print response.status_code, response.url
    print "Request was not redirected"


>>> import requests
>>> response = requests.get('')
>>> response.history
(<Response [302]>, <Response [302]>, <Response [302]>)
>>> for resp in response.history:
...     print resp.status_code, resp.url
>>> print response.status_code, response.url

the documentation has this blurb

r = requests.get('')
#returns instead of the http page you asked for 

This is answering a slightly different question, but since I got stuck on this myself, I hope it might be useful for someone else.

If you want to use allow_redirects=False and get directly to the first redirect object, rather than following a chain of them, and you just want to get the redirect location directly out of the 302 response object, then r.url won't work. Instead, it's the "Location" header:

r = requests.get('')
r.status_code  # 302
r.url  #, not https.
r.headers['Location']  # -- the redirect destination

I think requests.head instead of requests.get will be more safe to call when handling url redirect,check the github issue here:

r = requests.head(url, allow_redirects=True)

Need Your Help

how to solve a eqaution of a parabola that passes through 3 points?


how do you find a equation of a parabola that passes through theses points: ( .0344,.9285), ( .4014, 1.4672), (1.002, -0.313).

swing file selection on a Mac OSX

osx swing jfilechooser

I am using JFileChooser. On a Macintosh I get this crappy crippled dialog box. I'm used to seeing the Spotlight search box that helps me find which directory something is in; how can I enable this

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.