Python

Urllib.Parse.Urlsplit

In this post, you will learn how to use the urlsplit() function from the parse module of the urllib package. This function allows us to split a given URL resource into various segments.

Let’s jump in.

Function Syntax

The function syntax is as shown in the following:

urllib.parse.urlsplit(urlstring, scheme='', allow_fragments=True)

The function takes the URL to split the scheme to access the URL as the required parameters.

The function performs similar actions as the urlparse() function. However, it does not split the parameters from the URL.

The function then returns a named tuple with the items which can be accessed via their index or named attributes. The attributes are as follows:

Attribute Name Index Position Value
scheme 0 URL scheme
netloc 1 Network Location
path 2 Hierachical Path
query 3 Query Value
fragment 4 Fragement Identifier
username username
password password
hostname Host name
port Port number

Let us look at a practical example that illustrates how to use the function.

Practical Example:

Consider the following example code provided:

from urllib.parse import urlsplit
url = "https://username:password@localhost:9001/p;param1?query=test_query#frag"
parsed_url = urlsplit(url)
print("schema -> ", parsed_url.scheme)
print("netloc -> ", parsed_url.netloc)
print("path -> ", parsed_url.path)
print("query -> ", parsed_url.query)
print("fragment -> ", parsed_url.fragment)
print("username -> ", parsed_url.username)
print("password -> ", parsed_url.password)
print("hostname -> ", parsed_url.hostname)
print("port -> ", parsed_url.port)

The given code uses the urlsplit function to parse the URL into various segments. We can then print them out as shown in the following output:

schema ->  https
netloc ->  username:password@localhost:9001
path ->  /p;param1
query ->  query=test_query
fragment ->  frag
username ->  username
password ->  password
hostname ->  localhost
port ->  9001

Conclusion

In this post, we discussed using the urlsplit() function from the parse module which allows us to split a given URL into various segments. Feel free to explore the document to learn more.

Happy coding!

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list