Determining redirected URL in Python
Question: I make a little parser using HTMLparser and I would like to know where a link
is redirected. I don't know how to explain this, so please look this example:

On my page I have a link on the source: `http://www.myweb.com?out=147`, which
redirects to `"http://www.mylink.com"`. I can parse
`"http://www.myweb.com?out=147"` without any problems, but I don't know how to
have get `http://www.mylink.com`.

Thanks a lot for your help guys !


Answer: You can use [`urllib2`](https://docs.python.org/2/library/urllib2.html#module-
urllib2)
([`urllib.request`](https://docs.python.org/3/library/urllib.request.html#module-
urllib.request) in Python 3) and its
[`HTTPRedirectHandler`](https://docs.python.org/2/library/urllib2.html#httpredirecthandler-
objects) in order to find out where a URL will redirect you. Here's a function
that does that:

    
    
    import urllib2
    
    def get_redirected_url(url):
        opener = urllib2.build_opener(urllib2.HTTPRedirectHandler)
        request = opener.open(url)
        return request.url
    
    print get_redirected_url("http://google.com/")
    # prints "http://www.google.com/"
    

