Python | Split URL from Query Parameters
Last Updated :
09 Apr, 2023
Sometimes, while web development, we can come across a task in which we may require to perform a split of query parameters from URLs which is done by '?' character. This has application over web development as well as other domains which involve URLs. Lets discuss certain ways in which this task can be performed.
Method #1 : Using split()
This is one of the way in which we can solve this problem. We split by '?' and return the first part of split for result.
Python3
# Python3 code to demonstrate working of
# Split URL from Query Parameters
# Using split()
# initializing string
test_str = 'www.geeksforgeeks.org?is = best'
# printing original string
print("The original string is : " + str(test_str))
# Split URL from Query Parameters
# Using split()
res = test_str.split('?')[0]
# printing result
print("The base URL is : " + res)
Output : The original string is : www.geeksforgeeks.org?is=best
The base URL is : www.geeksforgeeks.org
Time Complexity: O(n) -> (split function)
Auxiliary Space: O(n)
Method #2 : Using rfind()
This is another way in which we need to perform this task. In this, we find the first occurrence of '?' from right and slice the string.
Python3
# Python3 code to demonstrate working of
# Split URL from Query Parameters
# Using rfind()
# initializing string
test_str = 'www.geeksforgeeks.org?is = best'
# printing original string
print("The original string is : " + str(test_str))
# Split URL from Query Parameters
# Using rfind()
res = test_str[:test_str.rfind('?')]
# printing result
print("The base URL is : " + res)
Output : The original string is : www.geeksforgeeks.org?is=best
The base URL is : www.geeksforgeeks.org
Time Complexity: O(n)
Auxiliary Space : O(n)
Method #3 : Using index().Finding index of '?' and then used string slicing
Python3
# Python3 code to demonstrate working of
# Split URL from Query Parameters
# Using index()
# initializing string
test_str = 'www.geeksforgeeks.org?is = best'
# printing original string
print("The original string is : " + str(test_str))
# Split URL from Query Parameters
# Using index()
res = test_str[0:test_str.index('?')]
# printing result
print("The base URL is : " + res)
OutputThe original string is : www.geeksforgeeks.org?is = best
The base URL is : www.geeksforgeeks.org
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #4 : Using operator.getitem(),index() methods
Approach
- Found index of ? using index() method
- Used operator.getitem(),slice() to extract the sliced string from start(0) to the index of ? and assigned to res variable
- Displayed the res variable
Python3
# Python3 code to demonstrate working of
# Split URL from Query Parameters
# Using index()
# initializing string
test_str = 'www.geeksforgeeks.org?is = best'
# printing original string
print("The original string is : " + str(test_str))
# Split URL from Query Parameters
# Using index()
import operator
res = operator.getitem(test_str,slice(0, test_str.index('?')))
# printing result
print("The base URL is : " + res)
OutputThe original string is : www.geeksforgeeks.org?is = best
The base URL is : www.geeksforgeeks.org
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #5 : Using urlparse function:
1.Import the urlparse function from the urllib.parse module.
2.Define the input URL string as test_str.
3.Use the urlparse function to parse the test_str URL into a ParseResult object.
4.Use the _replace method to create a new ParseResult object with the query parameter set to None.
5.Use the geturl method to generate a new URL string from the modified ParseResult object.
6.Print the new URL string to the console.
Python3
# Importing the urlparse function from the urllib.parse module
from urllib.parse import urlparse
# Defining the input URL string
test_str = 'http://www.geeksforgeeks.org?is=best'
# Using the urlparse function to parse the input URL into its component parts
parsed_url = urlparse(test_str)
# printing original string
print("The original string is : " + str(test_str))
# Using the _replace method to create a new parsed URL object with the query parameter set to None
# This effectively removes the query parameter from the URL
new_parsed_url = parsed_url._replace(query=None)
# Using the geturl method to generate a new URL string from the modified parsed URL object
new_url_str = new_parsed_url.geturl()
# Printing the new URL string
print(new_url_str)
OutputThe original string is : http://www.geeksforgeeks.org?is=best
http://www.geeksforgeeks.org
Time complexity:
Parsing the URL using the urlparse function has a time complexity of O(n), where n is the length of the input string.
Using the _replace method has a time complexity of O(1), as it simply creates a new ParseResult object with a modified query parameter.
Using the geturl method has a time complexity of O(n), where n is the length of the output URL string.
Overall, the time complexity of this code is O(n), where n is the length of the input and output strings.
Auxiliary Space:
The space complexity of this code is O(n), where n is the length of the input and output strings.
This is because the urlparse function creates a new ParseResult object that stores the various components of the URL (such as the scheme, netloc, path, query, and fragment).
The _replace method creates a new ParseResult object with a modified query parameter, and the geturl method generates a new URL string from this object.
Thus, the amount of space required by this code is proportional to the length of the input and output strings.
Method #6: Using re.split()
- Import the re module.
- Define a regular expression pattern to match the query parameters section of the URL (the part after the ? character).
- Use the re.split() function to split the URL using the regular expression pattern.
- The first element of the resulting list will be the base URL.
Python3
# Python3 code to demonstrate working of
# Split URL from Query Parameters
# Using re.split()
# import re module
import re
# initializing string
test_str = 'www.geeksforgeeks.org?is = best'
# printing original string
print("The original string is : " + str(test_str))
# Split URL from Query Parameters
# Using re.split()
pattern = r'\?' # regular expression pattern to match the query parameters section
res = re.split(pattern, test_str)[0]
# printing result
print("The base URL is : " + res)
OutputThe original string is : www.geeksforgeeks.org?is = best
The base URL is : www.geeksforgeeks.org
Time complexity: The time complexity of this method is O(n), where n is the length of the input string.
Auxiliary space: The space complexity of this method is O(n), where n is the length of the input string.
Similar Reads
Python | Key-Value to URL Parameter Conversion Many times, while working in the web development domain, we can encounter a problem in which we require to set as URL parameters some of the key-value pairs we have, either in form of tuples, or a key and value list. Let's discuss a solution for both cases. Method #1: Using urllib.urlencode() ( with
5 min read
Remove URLs from string in Python A regular expression (regex) is a sequence of characters that defines a search pattern in text. To remove URLs from a string in Python, you can either use regular expressions (regex) or some external libraries like urllib.parse. The re-module in Python is used for working with regular expressions. I
3 min read
Get the File Extension from a URL in Python Handling URLs in Python often involves extracting valuable information, such as file extensions, from the URL strings. However, this task requires careful consideration to ensure the safety and accuracy of the extracted data. In this article, we will explore four approaches to safely get the file ex
2 min read
Python program to Recursively scrape all the URLs of the website In this tutorial we will see how to we can recursively scrape all the URLs from the website Recursion in computer science is a method of solving a problem where the solution depends on solutions to smaller instances of the same problem. Such problems can generally be solved by iteration, but this ne
2 min read
Python | URL shortener using tinyurl API There are multiple APIs (e.g-bitly API, etc.) available for URL shortening service but in this article, we will be using Tinyurl API to shorten URLs. Though tinyURL has not officially released its API but we are going to use it unofficially. Here, we can put as input any number of URLs at a time and
3 min read
How to Pass Parameters in URL with Python Passing parameters in a URL is a common way to send data between a client and a server in web applications. In Python, this is usually done using libraries like requests for making HTTP requests or urllib .Let's understand how to pass parameters in a URL with this example.Example:Pythonimport urllib
2 min read