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
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.
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
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.