2

I am trying to go to a URL, which will return a code string in the redirected url.

Example: https://exampleurl.com

2-3 seconds afterwards I get redirected to https://newurl.com/stuffhere?code=97412098512yidfh480b14r

Is there a way to return the the new URL?

glhr
  • 3,982
  • 1
  • 13
  • 24
Kusnan
  • 115
  • 7
  • Possible duplicate of [Python Requests library redirect new url](https://stackoverflow.com/questions/20475552/python-requests-library-redirect-new-url) – glhr Apr 22 '19 at 09:18

1 Answers1

2

From the official docs:

The Response.history list contains the Response objects that were created in order to complete the request. The list is sorted from the oldest to the most recent response.

Here's an example that prints the URLs that led to the final URL:

import requests
response = requests.get('http://httpbin.org/redirect/3')
for resp in response.history:
    print(resp.url)

To get the final URL only, you can simply do:

print(response.url)
glhr
  • 3,982
  • 1
  • 13
  • 24