簡體   English   中英

Python 請求無法獲得重定向的 URL

[英]Python Requests unable to get redirected URL

我定義了以下函數來使用 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/.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'


是否因為重定向等待 5 秒而失敗? 如果是這樣,我們如何將其納入?

我已經嘗試過像這樣這樣的其他答案。 卻無法破解。

很簡單: r.headers 沒有 'Location' 鍵。 您可能使用了錯誤的密鑰。

編輯:您要使用請求瀏覽的站點受到保護。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM