dot*_*hen 4 python python-requests
我有时需要使用提供的Beautiful Soup和Requests URL进行解析:
当然,这些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.
谢谢.
它位于对象的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)
您可以在"快速入门"页面中轻松找到此信息.