user2293094 user2293094 - 1 month ago 10x
HTML Question

Using python to expand a link

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

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


Python 2:

>>> import urllib2
>>> print urllib2.urlopen('').url

You can also use the geturl() method:

>>> import urllib2
>>> print urllib2.urlopen('').geturl()

And, for Python 3:

>>> from urllib.request import urlopen
>>> print(urlopen('').geturl())