How to Convert Relative URL to Absolute URL Using Python

When we fetch a url page using python, one important thing is how to change a relative url to an absolute one. For example, if you are fetching a url: http://www.example.com/adsense-test/2012/10/index.html, and there is a relative url in this page: ../../book/images/02.png, at this situation, you must convert it to an absolute one so that you can continue to fetch.

Here is an example to convert a relative url to an absolute one:

import urlparse

def getAbsoluteURL(relative_url, base_url):
    
    url = urlparse.urljoin(base_url, relative_url) 
    
    url = reduce(lambda r,x: r.replace(x[0], x[1]), [('/../', '/'), ('\n', ''), ('\r', '')], url)
    return url

relurl = './nmypic.png'
absurl = 'http://www.example.com/2012/'
print getAbsoluteURL(relurl, absurl)

relurl = '../nmypic.png'
absurl = 'http://www.example.com/2012/'
print getAbsoluteURL(relurl, absurl)

relurl = '../../nmypic.png'
absurl = 'http://www.example.com/2012/10/'
print getAbsoluteURL(relurl, absurl)

relurl = '../../book/images/02.png'
absurl = 'http://www.example.com/adsense-test/2012/10/index.html'
print getAbsoluteURL(relurl, absurl)

The result is:

http://www.example.com/2012/nmypic.png
http://www.example.com/nmypic.png
http://www.example.com/nmypic.png
http://www.example.com/adsense-test/book/images/02.png

, , ,