按重定向顺序返回最后一个URL

dot*_*hen 4 python python-requests

我有时需要使用提供的Beautiful Soup和Requests URL进行解析:

http://bit.ly/sdflksdfwefwe

http://stup.id/sdfslkjsfsd

http://0.r.msn.com/sdflksdflsdj

当然,这些URL通常会"解析"为规范URL http://real-website.com/page.html.如何获取解析/重定向链中的最后一个URL?

我的代码通常如下所示:

from bs4 import BeautifulSoup
import requests

response = requests.get(url)
soup = bs4.BeautifulSoup(response.text, from_encoding=response.encoding)
canonical_url = response.??? ## This is what I need to know
Run Code Online (Sandbox Code Playgroud)

请注意,我并不是要查询http://bit.ly/bllsht它的位置,而是当我使用Beautiful Soup来解析它返回的页面时,还要获取重定向链中最后一个的规范URL.

谢谢.

kir*_*gin 6

它位于对象的url属性中response.

>>> response = requests.get('http://bit.ly/bllsht')
>>> response.url
  > u'http://www.thenews.org/sports/well-hey-there-murray-state-1-21-11-1.2436937'
Run Code Online (Sandbox Code Playgroud)

您可以在"快速入门"页面中轻松找到此信息.

  • 感谢您使用链接进行编辑.在我看来,`url`属性将是`get()`方法中指定的URL. (2认同)