Return last URL in redirect sequence

I sometimes have to deal with Beautiful Soup and Requests URLs, which are provided as such:

http://bit.ly/sdflksdfwefwe

http://stup.id/sdfslkjsfsd

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

Of course, these URLs usually "resolve" the canonical URL as http://real-website.com/page.html . How can I get the last url in the permission / redirect chain?

My code usually looks like this:

 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 

Please note that I do not want to request http://bit.ly/bllsht to see where it is going, but when I use Beautiful Soup to already parse the returned page, to also get the canonical URL, which was the last to redirect the chain.

Thanks.

+4
source share
1 answer

In the url attribute of your response object.

 >>> 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' 

You can easily find this information on the Quick Start page.

+6
source

Source: https://habr.com/ru/post/1485771/


All Articles