繁体   English   中英

用 python 一次替换多个模式

[英]Replace multiple patterns once at a time with python

所以我想做的基本上是我有一个包含多个参数的 url 列表,例如:

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

我想得到的是这样的:

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

就像我想遍历每个参数(基本上是“=”和“&”的每个匹配项)并每次替换每个值一个。 先感谢您。

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

['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']

我已经解决了:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM