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.
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.
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/
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/