Example:
http://example.com/?a=text&q2=text2&q3=text3&q2=text4
After removing "q2", it will return:
http://example.com/?q=text&q3=text3
In this case, there were multiple "q2" and all have been removed.
import re
q ="http://example.com/?a=text&q2=text2&q3=text3&q2=text4"
todelete="q2"
#Delete every query string matching the pattern
r = re.sub(r''+todelete+'=[a-zA-Z_0-9]*\&*',r'',q)
#Delete the possible trailing #
r = re.sub(r'&$',r'',r)
print r
Or simply put, just use url_query_cleaner()
from w3lib.url
from w3lib.url import url_query_cleaner
url = 'http://example.com/?a=text&q2=text2&q3=text3&q2=text4'
url_query_cleaner(url, ('q2'), remove=True)
Output: http://example.com/?a=text&q3=text3
query_string = "https://example.com/api/api.php?user=chris&auth=true"
url = query_string[:query_string.find('?', 0)]
Or you could just use strip
>>> l='http://example.com/?a=text&q2=text2&q3=text3&q2=text4'
>>> l.strip('&q2=text4')
'http://example.com/?a=text&q2=text2&q3=text3'
>>>
To remove all query string parameters:
from urllib.parse import urljoin, urlparse
url = 'http://example.com/?a=text&q2=text2&q3=text3&q2=text4'
urljoin(url, urlparse(url).path) # 'http://example.com/'
For Python2, replace the import with:
from urlparse import urljoin, urlparse
Output: