Detect destination of shortened, or "tiny" url Detect destination of shortened, or "tiny" url python python

Detect destination of shortened, or "tiny" url


The easiest way to get the destination of a shortened URL is with urllib. Given that the short URL is valid (response code 200), the URL be returned to you.

>>> import urllib>>> resp = urllib.urlopen('http://bit.ly/bcFOko')>>> resp.getcode()200>>> resp.url'http://mrdoob.com/lab/javascript/harmony/'

And that's that!


(AFAIK) Most url shorteners keep track of urls already shortened, so several requests to the same engine with the same URL will return the same short code.

As has been suggested, the best way to extract the real url is to read the headers from a response to a request for the shortened URL. However, some shortening services (eg bit.ly) provide an API method to return the long url


  1. Do a list of the most used URL-shorteners and expand it while you discover new ones, then check a link for one item of the list.

  2. You do not know where the URL points to unless you follow it, so best way to do this should be to follow the shortened url and extract the http header of the response to see where it heads to.

I guess with 100 requests per second you could surely go into trouble (I guestt the worst that can happen is they blacklist your IP as a spammer).