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 below:
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 will then return a named tuple with the items which can be accessed via their index or named attributes. The attributes are as shown:
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 illustrating how to use the function.
Practical Example
Consider the example code provided below.
>>> from urllib.parse import urlsplit
>>> url = "https://username:password@localhost:9001/p;param1query=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 code above uses the urlsplit function to parse the URL into various segments. We can then print them out as shown in the output below:
schema -> https
netloc -> username:password@localhost:9001
path -> /p;param1
query -> query=test_query
fragment -> frag
username -> username
password -> password
hostname -> localhost
port -> 9001
Terminating
In this post, we discussed using the urlsplit()
function from the parse module, allowing us to split a given URL into various segments.