0

I have defined the following function to get the redirected URLs using Requests library. However i get the error KeyError: 'location'

def get_redirected_url(r_url):
    r = requests.get(r_url, allow_redirects=False)
    url = r.headers['Location']
    return url

Calling the function

get_redirected_url('http://omgili.com/ri/.wHSUbtEfZQujfav8g98PjRMi_ogV.5EwBTfg476RyS2Gqya3tDAwNIv8Yi8wQ9AK4.U2mxeyq2_xbUjqsOx8NYY8r0qgxD.4Bm2SrouZKnrg1jqRxEfVmGbtTaKTaaDJtOjtS46fYr6A5UJoh9BYxVtDGJIsbSfgshRXR3FVr4-')

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 3, in get_redirected_url
  File "/home/user/PycharmProjects/untitled/venv/lib/python3.6/site-packages/requests/structures.py", line 54, in __getitem__
    return self._store[key.lower()][1]
KeyError: 'location'


Is it failing because the redirection waits for 5 seconds? If so, how do we incorporate that as well?

I have tried the other answers like this and this. But unable to crack it.

Salih
  • 391
  • 1
  • 13

1 Answers1

0

It is simple: r.headers doesn't have 'Location' key. You may have use the wrong key.

Edit: the site you want to browse with requests is protected.

Matteo Bianchi
  • 434
  • 1
  • 4
  • 19