Menu Close

How to redirect to a new URL with Python Requests library?

Sometimes, we want to redirect to a new URL with Python Requests library.

In this article, we’ll look at how to redirect to a new URL with Python Requests library.

How to redirect to a new URL with Python Requests library?

To redirect to a new URL with Python Requests library, we can get the redirect URL with the response object’s url property.

For instance, we write

import requests

r = requests.get('http://www.github.com')
print(r.url)

to call requests.get with a URL.

Then we get the URL that the URL that we make the request redirects to with r.url.

Conclusion

To redirect to a new URL with Python Requests library, we can get the redirect URL with the response object’s url property.

Posted in Python, Python Answers