Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I remove a query string from URL using Python

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.

like image 780
user990611 Avatar asked Oct 12 '11 02:10

user990611


People also ask

How do I remove a string from URL in Python?

Use the re. sub() method to remove URLs from text, e.g. result = re. sub(r'http\S+', '', my_string) .

How do I remove a query param from URL in Google Analytics?

One way to remove query parameters from pages is through the View Settings. Under Admin > View Settings > Exclude Query Parameters, list the query parameters that you want to exclude from your page paths.


3 Answers

import sys

if sys.version_info.major == 3:
    from urllib.parse import urlencode, urlparse, urlunparse, parse_qs
else:
    from urllib import urlencode
    from urlparse import urlparse, urlunparse, parse_qs

url = 'http://example.com/?a=text&q2=text2&q3=text3&q2=text4&b#q2=keep_fragment'
u = urlparse(url)
query = parse_qs(u.query, keep_blank_values=True)
query.pop('q2', None)
u = u._replace(query=urlencode(query, True))
print(urlunparse(u))

Output:

http://example.com/?a=text&q3=text3&b=#q2=keep_fragment
like image 101
lazy1 Avatar answered Oct 01 '22 14:10

lazy1


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
like image 28
png Avatar answered Oct 02 '22 14:10

png


Isn't this just a matter of splitting a string on a character?

>>> url = http://example.com/?a=text&q2=text2&q3=text3&q2=text4
>>> url = url.split('?')[0]
'http://example.com/'
like image 21
Clarius Avatar answered Oct 01 '22 14:10

Clarius