Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determining redirected URL in Python

I made 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 get http://www.mylink.com.

like image 470
MisterU Avatar asked Apr 04 '11 12:04

MisterU


People also ask

How do I check 301 redirects?

To test whether you've set up your 301 redirect correctly, type your customized URL into your browser's address bar. If everything is set up correctly, you should be redirected to the defined destination page.

How do I get the URL in Python?

You can get the current url by doing path_info = request. META. get('PATH_INFO') http_host = request.


1 Answers

You can use urllib2 (urllib.request in Python 3) and its HTTPRedirectHandler 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/"
like image 200
Jeremy Avatar answered Sep 19 '22 22:09

Jeremy