how to get redirect url using python requests

Question:

r = requests.get('http://techtv.mit.edu/videos/1585-music-session-02/download.source') 
for i in r.history:
    print(i.url) 

I think it should print out the history, but it doesn’t, the above url points to a video, but I cannot get it, anyone help? Thank you

Asked By: 1a1a11a

||

Answers:

To get the resultant URL after you’ve been redirected, you can do r.url.

r = requests.get('https://youtu.be/dQw4w9WgXcQ') 
print(r.url) # https://www.youtube.com/watch?v=dQw4w9WgXcQ&feature=youtu.be

r.history is for URLs prior to the final one, so it’s only returning your original URL because you were only redirected once.

Answered By: Aaron Christiansen
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.