user2293094 user2293094 - 11 months ago 101
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.

Answer Source

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())