Replace multiple patterns once at a time with python

Question:

so what i wanna do is basically i have a list of urls with multiple parameters, such as:

https://www.somesite.com/path/path2/path3?param1=value1&param2=value2

and i would want to get is something like this:

https://www.somesite.com/path/path2/path3?param1=PAYLOAD&param2=value2
https://www.somesite.com/path/path2/path3?param1=value1¶m2=PAYLOAD

like i wanna iterate through every parameter (basically every match of "=" and "&") and replace each value one per time. Thank you in advance.

Asked By: FozenOption

||

Answers:

from urllib.parse import urlparse
import re

urls = ["https://www.somesite.com/path/path2/path3?param1=value1&param2=value2&param3=value3",
        "https://www.anothersite.com/path/path2/path3?param1=value1&param2=value2&param3=value3"]
parseds = [urlparse(url) for url in urls]
newurls = []
for parsed in parseds:
    params = parsed[4].split("&")
    for i, param in enumerate(params):
        newparam = re.sub("=.+", "=PAYLOAD", param)
        newurls.append(
            parsed[0] +
            "://" +
            parsed[1] +
            parsed[2] +
            "?" +
            parsed[4].replace(param, newparam)
            )

newurls is

['https://www.somesite.com/path/path2/path3?param1=PAYLOAD&param2=value2&param3=value3',
 'https://www.somesite.com/path/path2/path3?param1=value1&param2=PAYLOAD&param3=value3',
 'https://www.somesite.com/path/path2/path3?param1=value1&param2=value2&param3=PAYLOAD',
 'https://www.anothersite.com/path/path2/path3?param1=PAYLOAD&param2=value2&param3=value3',
 'https://www.anothersite.com/path/path2/path3?param1=value1&param2=PAYLOAD&param3=value3',
 'https://www.anothersite.com/path/path2/path3?param1=value1&param2=value2&param3=PAYLOAD']
Answered By: Eftal Gezer

I’ve solved it:

from urllib.parse import urlparse

url = "https://github.com/search?p=2&q=user&type=Code&name=djalel"

parsed = urlparse(url)
query = parsed.query
params = query.split("&")
new_query = []
for param in params:
    l = params.index(param)
    param = str(param.split("=")[0]) + "=" + "PAYLOAD"
    params[l] = param
    new_query.append("&".join(params))
    params = query.split("&")

for query in new_query:
        print(str(parsed.scheme) + '://' + str(parsed.netloc) + str(parsed.path) + '?' + query)

Output:

https://github.com/search?p=PAYLOAD&q=user&type=Code&name=djalel
https://github.com/search?p=2&q=PAYLOAD&type=Code&name=djalel
https://github.com/search?p=2&q=user&type=PAYLOAD&name=djalel
https://github.com/search?p=2&q=user&type=Code&name=PAYLOAD
Answered By: FozenOption
Categories: questions Tags: , , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.