4

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?

revy
  • 3,945
  • 7
  • 40
  • 85

1 Answers1

2

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/#/'
RafalS
  • 5,834
  • 1
  • 20
  • 25