Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get redirect url using python requests [duplicate]

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

like image 505
1a1a11a Avatar asked Mar 17 '16 20:03

1a1a11a


People also ask

How do I get the redirected URL in Python?

Use Python urllib Library To Get Redirection URL. request module. Define a web page URL, suppose this URL will be redirected when you send a request to it. Get the response object. Get the webserver returned response status code, if the code is 301 then it means the URL has been redirected permanently.

How do I get URL redirect code?

You can have a frontend app under the redirect URI. Then you can run the code you pasted in your browser. You can use javascript to get the query string of the current page (e.g. by calling window. location.search and then splitting the string using & as the delimiter).

Can you double redirect?

Double 301 Redirect This hack still works today. A double 301 redirect allows you to pass the link juice to the final domain without passing any penalty. It's so simple, you might be skeptical, but the proof is in the pudding and I'll be walking you through it with a live example.


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

like image 182
Aaron Christiansen Avatar answered Sep 28 '22 08:09

Aaron Christiansen