-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

5

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
  • 80,261
  • 9
  • 108
  • 134
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
  • 46,308
  • 56
  • 161
  • 219
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
  • 11,955
  • 1
  • 39
  • 46