如何在python中修改url查询的值?

6qqygrtg  于 2023-06-04  发布在  Python
关注(0)|答案(4)|浏览(285)
url = "http://www.example.com?type=a&type1=b&type2=c"
 urllist = get_urllist(url)
 trigger = ["'or '1'='1'"," 'OR '1'='2'","'OR a=a"]

def get_urllist(url): 
    url_parsed = urlparse.urlparse(url)
    #extract the query parameters of the URL 
    query =  urlparse.parse_qs(url_parsed.query)
    #get the list of query 
    query_list = query_list(query)
    #Get Base url 
    url = urlparse._replace(query=None).geturl()
    #modify url to get url_list 
    for query in query_list : 
       # change the original query to get the expected result 

 return url_list 

def query_list(query):
     for t in trigger:
         for key, value in query.items():
            query[key] += t
         query_list.append(query) 

     return query_list

如何通过更改查询参数值返回URL列表?
原始url =“http://www.example.com?type=a&type1=b&type2=c
预期结果:
Copyright © 2018 - 2019 www.http://www.example.com?type=a.com版权所有

rmbxnbpk

rmbxnbpk1#

在Python2.x

你可以使用urlparse.urlparse函数和ParseResult._replace方法:

import urlparse
url = "http://www.example.com?type=a&type1=b&type2=c"
trigger = ["'or '1'='1'"," 'OR '1'='2'","'OR a=a"]

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))

备注

    • Python 3**中的urlparse模块重命名为urllib.parse2to3工具将在将源代码转换为Python 3时自动调整导入。
      在Python3.x

你也可以使用urlparse.urlparse函数。

import urllib.parse as urlparse
url = "http://www.example.com?type=a&type1=b&type2=c"
trigger = ["'or '1'='1'"," 'OR '1'='2'","'OR a=a"]

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))

演示输出:

["http://www.example.com?type=a'or '1'='1'&type1=b'or '1'='1'&type2=c'or '1'='1'", "http://www.example.com?type=a 'OR '1'='2'&type1=b 'OR '1'='2'&type2=c 'OR '1'='2'", "http://www.example.com?type=a'OR a=a&type1=b'OR a=a&type2=c'OR a=a"]
wmomyfyw

wmomyfyw2#

您可以使用furl包。

from furl import furl

url = furl("http://www.example.com?type=a&type1=b&type2=c")
url.set({"type": "a'or '1'='1'"})
url.url

给出输出:http://www.example.com?type=a%27or+%271%27%3D%271%27
并解码:http://www.example.com?type=a'or '1'='1'

j2datikz

j2datikz3#

举个简单的例子:

def patch_url(url, **kwargs):
    from urllib.parse import urlparse, urlencode, parse_qsl
    return urlparse(url)._replace(query=urlencode(
        dict(parse_qsl(urlparse(url).query), **kwargs))).geturl()

assert patch_url("https://httpbin.org/get?hello=world", hello="human") \
       == "https://httpbin.org/get?hello=human"
mrphzbgm

mrphzbgm4#

为了避免使用私有方法_replace(),我只是创建了一个新的SplitResult,在必要的地方替换了旧的参数。

p = parse.urlsplit(url)
url = parse.SplitResult("https", *p[1:]).geturl()

我使用的是urlsplit(),它返回SplitResult,但我想你可以对从urlparse()返回的ParseResult做同样的事情。两者都被命名为元组。docs中描述了所有内容
对于具体的查询,还执行parse_qs()以获取参数的dict,并执行urlencode()以获取查询字符串。

>>> parse.urlencode({"a":1, "b":"yes", "c":[1,2,3]}, doseq=False)
'a=1&b=yes&c=%5B1%2C+2%2C+3%5D'
>>> parse.urlencode({"a":1, "b":"yes", "c":[1,2,3]}, doseq=True)
'a=1&b=yes&c=1&c=2&c=3'

相关问题