Home > OS >  Replace multiple patterns once at a time with python
Replace multiple patterns once at a time with python

Time:10-01

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&param2=PAYLOAD

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

CodePudding user response:

I think the parameter order doesn't matter.

from urllib.parse import urlparse

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

newurls is

['https://www.somesite.com/path/path2/path3?param2=value2&param1=PAYLOAD',
 'https://www.somesite.com/path/path2/path3?param1=value1&param2=PAYLOAD',
 'https://www.anothersite.com/path/path2/path3?param2=value2&param1=PAYLOAD',
 'https://www.anothersite.com/path/path2/path3?param1=value1&param2=PAYLOAD']
  • Related