Categories
Python Answers

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

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *