How can I unshorten a URL?

Posted on

Question :

How can I unshorten a URL?

I want to be able to take a shortened or non-shortened URL and return its un-shortened form. How can I make a python program to do this?

Additional Clarification:

  • Case 1: shortened –> unshortened
  • Case 2: unshortened –> unshortened

e.g. in the input array should be in the output array
e.g. in the input array should be in the output array

Asked By: Andrew


Answer #1:

Send an HTTP HEAD request to the URL and look at the response code. If the code is 30x, look at the Location header to get the unshortened URL. Otherwise, if the code is 20x, then the URL is not redirected; you probably also want to handle error codes (4xx and 5xx) in some fashion. For example:

# This is for Py2k.  For Py3k, use http.client and urllib.parse instead, and
# use // instead of / for the division
import httplib
import urlparse

def unshorten_url(url):
    parsed = urlparse.urlparse(url)
    h = httplib.HTTPConnection(parsed.netloc)
    h.request('HEAD', parsed.path)
    response = h.getresponse()
    if response.status/100 == 3 and response.getheader('Location'):
        return response.getheader('Location')
        return url
Answered By: Adam Rosenfield

Answer #2:

Using requests:

import requests

session = requests.Session()  # so connections are recycled
resp = session.head(url, allow_redirects=True)
Answered By: GermainZ

Answer #3: has an api that lets you send a JSON or XML request and get the full URL returned.

Answered By: user387049

Answer #4:

Open the url and see what it resolves to:

>>> import urllib2
>>> a = urllib2.urlopen('')
>>> print a.url
>>> a = urllib2.urlopen('')
>>> print a.url
Answered By: hughdbrown

Answer #5:

To unshort, you can use requests. This is a simple solution that works for me.

import requests
url = ""

site = requests.get(url)
Answered By: Daniel Cambría

Answer #6:

If you are using Python 3.5+ you can use the Unshortenit module that makes this very easy:

from unshortenit import UnshortenIt
unshortener = UnshortenIt()
uri = unshortener.unshorten('')
Answered By: fmarm

Answer #7:

sudo pip install urlclean
Answered By: DmitrySandalov

Answer #8:

Here a src code that takes into account almost of the useful corner cases:

  • set a custom Timeout.
  • set a custom User Agent.
  • check whether we have to use an http or https connection.
  • resolve recursively the input url and prevent ending within a loop.

The src code is on github @

comments are welcome …

import logging

class UnShortenUrl:
    def process(self, url, previous_url=None):'Init url: %s'%url)
        import urlparse
        import httplib
            parsed = urlparse.urlparse(url)
            if parsed.scheme == 'https':
                h = httplib.HTTPSConnection(parsed.netloc, timeout=TIMEOUT)
                h = httplib.HTTPConnection(parsed.netloc, timeout=TIMEOUT)
            resource = parsed.path
            if parsed.query != "": 
                resource += "?" + parsed.query
                          headers={'User-Agent': 'curl/7.38.0'}
                response = h.getresponse()
                import traceback
                return url

  'Response status: %d'%response.status)
            if response.status/100 == 3 and response.getheader('Location'):
                red_url = response.getheader('Location')
      'Red, previous: %s, %s'%(red_url, previous_url))
                if red_url == previous_url:
                    return red_url
                return self.process(red_url, previous_url=url) 
                return url 
            import traceback
            return None
Answered By: Amir Krifa

Leave a Reply

Your email address will not be published. Required fields are marked *