Convert Relative URL to Absolute URL in Python – Python Tutorial

By | July 9, 2019

Converting a relative url to an absolute one is very important when you are crawling a site, which can allow you to continue to crawl. In this tutorial, we will introduce you how to convert.

http url example


from urllib.parse import urljoin, urldefrag

Create a base url and a relative url

base_url = ''
relative_url = '/calculate-spearmans-correlation-coefficient-for-beginners-numpy-tutorial/#hello'

Convert relative url to absolute

url = urljoin(base_url, relative_url)
print (url)

The output is:

If you want to remove fragment in url, you can do like this.

Remove fragment in url

ux = urldefrag(url)
print (ux[0])

The output is: