-1

How can I use Python to take a bit.ly link and return the fully expanded link?

If the link inputted is not a bit.ly link, the original link should be returned.

Tomero
  • 183
  • 2
  • 10

3 Answers3

6

Python 2:

>>> import urllib2
>>> print urllib2.urlopen('http://bit.ly/1cPIdPg').url
http://stackoverflow.com/

You can also use the geturl() method:

>>> import urllib2
>>> print urllib2.urlopen('http://bit.ly/1cPIdPg').geturl()

And, for Python 3:

>>> from urllib.request import urlopen
>>> print(urlopen('http://bit.ly/1cPIdPg').geturl())
http://stackoverflow.com/
mhawke
  • 84,695
  • 9
  • 117
  • 138
2

You can do this using the urllib module

import urllib
response = urllib.urlopen('http://bit.ly/1mlEbqY')
print response.url

Outputs:

http://stackoverflow.com/questions/24689592/using-python-to-expand-a-bit-ly-link
Andy
  • 49,085
  • 60
  • 166
  • 233
0

It can be done using requests library of Python. Below is the code

import requests
r = requests.get('http_tiny_url_for_stackoverflow_or_any')
print r.url

Output:

http://stackoverflow.com/
Anand Tripathi
  • 14,556
  • 1
  • 47
  • 52