-2
import urllib.parse as urlparse
url = "http://www.example.com?type=aaaaaaa&type1=bbbbbbb&type2=cccccccc"
trigger = ["value1","value2","value3"]

parsed = urlparse.urlparse(url)
querys = parsed.query.split("&")
result = []
for pairs in trigger:
    new_query = "&".join([ "{}{}".format(query, pairs) for query in querys])
    parsed = parsed._replace(query=new_query)
    result.append(urlparse.urlunparse(parsed))

print(result)

How to return a list of URLs by replacing the query parameter values?

Output Result :

["http://www.example.com?type=aaaaavalue1&type1=bbbbbbvalue1&type2=ccccccccvalue1", "http://www.example.com?type=aaaaavalue2&type1=bbbbbbvalue2&type2=ccccccccvalue2", "http://www.example.com?type=aaaaavalue3&type1=bbbbbbvalue3&type2=ccccccccvalue3"]

Expected Result:

["http://www.example.com?type=value1&type1=value1&type2=value1", "http://www.example.com?type=value2&type1=value2&type2=value2", "http://www.example.com?type=value3&type1=value3&type2=value3"]

I just want to replace URL parameter values with the custom parameter values and do not want to append them.

  • 1
    Your URLs look _wrong_. `"http://www.example.com?type='or '1'='1'` has an ODD number of `'`. Use a UrlParser to modify your params. – Patrick Artner Mar 01 '22 at 08:28

1 Answers1

1

You can use the function replace

url = "http://www.example.com?type=aaaaaaa&type1=bbbbbbb&type2=cccccccc"
trigger = []
for i in range(url.count("=")):
    trigger.append("value{}".format(i+1))

urls = []
start = [pos for pos, char in enumerate(url) if char == "="]
end   = [pos for pos, char in enumerate(url) if char == "&"]
end.append(len(url))
for i in range(len(trigger)):
    urls.append(url.replace(url[start[0]+1:end[0]],trigger[i]).replace(url[start[1]+1:end[1]],trigger[i]).replace(url[start[2]+1:end[2]],trigger[i]))


>>> urls
['http://www.example.com?type=value1&type1=value1&type2=value1',
 'http://www.example.com?type=value2&type1=value2&type2=value2',
 'http://www.example.com?type=value3&type1=value3&type2=value3']
Edoardo Berardo
  • 142
  • 2
  • 9
  • I need to detect parameter value(it can be anything) and then replace it with my custom value. – vipin panchal Mar 04 '22 at 03:12
  • The parameter value can be anything. aaaaaa is just an example. – vipin panchal Mar 04 '22 at 06:36
  • Okay okay, I updated the code – Edoardo Berardo Mar 04 '22 at 08:26
  • Hi Vipin, I updated. Now it should work for what you explained me – Edoardo Berardo Mar 04 '22 at 14:30
  • Hi Vipin, I don't understand what do you mean, can you edit with another example? – Edoardo Berardo Mar 07 '22 at 12:44
  • The code I provide works for n parameters, e.g. if you have n=4 parameters: url = "http://www.example.com?type=aaaaaaa&type1=bbbbbbb&type2=cccccccc&type3=cccccccc" and trigger = ["value1","value2","value3", "value4"] it works – Edoardo Berardo Mar 07 '22 at 12:47
  • Plz check my code. In my code you can put n number of parameters and you do not need to update the trigger list. My code is doing right except it put the trigger value after the parameter value while i want to replace the parameter. In your code i have to update the trigger list too, According to your code if ther are 5 parameters, i have to put 5 items in the trigger list – vipin panchal Mar 08 '22 at 01:53
  • I updated the code so that "trigger" is filled depending on your link. However, in the code you provide above you cannot put n number of parameters (trigger is defined as three parameters. – Edoardo Berardo Mar 08 '22 at 11:41
  • https://example.com/?param1=value1&param2=value2 &........nparam=nvalue. There can be n number of parameters. I just need to replace parameter values with my custom values. For ex: https://example.com/?param1=mycustomvalue1&param2=mycustomvalue2 &........nparam=nmycustomvalue – vipin panchal Mar 26 '22 at 08:22