Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python how to get the final destination after redirections

I am analyzing url phishing data, some urls could have several redirections (301, 302).

I can get the final destination and number of redirections using curl:

curl -Ls -o /dev/null -w "%{num_redirects},%{url_effective}" <url>

Doing the same thing with python requests:

import requests

r = requests.get(url, allow_redirects=True)

if r.history:
  print(f'{len(r.history)},{r.history[-1].url}')

I found that using requests history doesn't give me the final destination (although the downloaded content is the same respect to curl).

For example given the url (this is a legitimate url, I swear) https://ludik.xyz/music, this is what I got with curl:

1,https://ludik.herokuapp.com/#/

This is what I got in python:

1,https://ludik.xyz/music

How can I get the final destination after all redirections in python?

like image 386
revy Avatar asked Oct 16 '25 13:10

revy


1 Answers

The final url is set on the response object:

In [5]: import requests 
   ...:  
   ...: r = requests.get("https://ludik.xyz/music")                                                                                                                                                                                           

In [8]: r.url                                                                                                                                                                                                                                 
Out[8]: 'https://ludik.herokuapp.com/#/'
like image 148
RafalS Avatar answered Oct 19 '25 02:10

RafalS