Enabling redirects in urllib.requests

Viewed 92

I want to use urllib.requests, but I want the requests to allow redirects.

def get_html(url):
        req = Request(url)
        html = urlopen(req).read()
        return html 

I want code like this:

def get_html(url):
        req = Request(url, allow_redirects=True)
        html = urlopen(req).read()
        return html
1 Answers

Why don't you use requests? You can get the url that redirects, then after getting the page, get the url attribute:

import requests
r = requests.get(Your_URL)
#this will give you the destination url:
print(r.url)
Related