Python请求无法获取重定向的URL

Python请求无法获取重定向的URL,python,python-requests,Python,Python Requests,我已经定义了以下函数来使用请求库获取重定向的URL。但是我得到了错误KeyError:“location” def get_redirected_url(r_url): r = requests.get(r_url, allow_redirects=False) url = r.headers['Location'] return url 调用函数 get_redirected_url('http://omgili.com/ri/.wHSUbtEfZQujfav8g98

我已经定义了以下函数来使用请求库获取重定向的URL。但是我得到了错误
KeyError:“location”

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

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'


get\u redirected\u url('http://omgili.com/ri/.wHSUbtEfZQujfav8g98PjRMi_ogV.5EwBTfg476RyS2Gqya3tDAwNIv8Yi8wQ9AK4.U2mxeyq2_xbUjqsOx8NYY8r0qgxD.4Bm2SrouZKnrg1jqRxEfVmGbtTaKTaaDJtOjtS46fYr6A5UJoh9BYxVtDGJIsbSfgshRXR3FVr4-')
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“”,第3行,在get_redirected_url中
文件“/home/user/PycharmProjects/untitled/venv/lib/python3.6/site packages/requests/structures.py”,第54行,在__
返回self.\u存储[key.lower()][1]
KeyError:“位置”
是否因为重定向等待5秒而失败?如果是,我们如何将其纳入


我试过其他答案,比如和。但是无法破解它。

很简单:r.headers没有“Location”键。你可能用错钥匙了


编辑:您希望通过请求浏览的网站受到保护。

您传递的url是否正确,因为您的代码工作正常,正在重定向。