Python - Building URLs

Neha Kumawat

9 months ago

The requests module can help us build the URLs and manipulate the URL value dynamically. Any sub-directory of the URL can be fetched programmatically and then some parts of it can be substituted with new values to build new URLs.


The below example uses urljoin to fetch the different subfolders in the URL path. The urljoin method is used to add new values to the base URL.
from requests.compat import urljoin
url_query = urljoin(base,'?vers=1.0')
url_sec = urljoin(url_query,'#section-5.4')
When we run the above program, we get the following output −

Split the URLS

The URLs can also be split into many parts beyond the main address. The additional parameters which are used for a specific query or tags attached to the URL are separated by using the urlparse method as shown below.
from requests.compat import urlparse
url1 = ''
When we run the above program, we get the following output −

ParseResult(scheme='https', netloc='', path='/2/py-modindex.html', params='', query='', fragment='cap-f')
ParseResult(scheme='https', netloc='', path='/2/search.html', params='', query='q=urlparse', fragment='')
To learn more about python, visit the InsideAIML page.
I hope you enjoyed reading this article and finally, you came to know about Python - Building URLs.
For more such blogs/courses on data science, machine learning, artificial intelligence and emerging new technologies do visit us at InsideAIML.
Thanks for reading…
Happy Learning…

Submit Review