如何在python中更改参数的值?

时间:2018-03-05 10:36:54

标签: python python-2.7

注意:没有修复网址。意味着始终无法看到此URL。我想要适用于所有网址的代码。

对于前,http://januapp.com/demo/search.php?search=aaa
        http://januapp.com/demo/search.php?other=aaa

现在我想将其更改为

http://januapp.com/demo/search.php?search=bbb http://januapp.com/demo/search.php?other=bbb

我不知道怎么办?

我试过这个

import optparse
import requests
import urlparse


parser = optparse.OptionParser() 

parser.add_option("-t","--Host", dest="Target", help="Please provide the target", default="true") 

options, args = parser.parse_args() 

url = options.Target 

xss = [] 
xss.append("bbb")  

try:

    url2 =urlparse.urlparse(url)    
    print url2
    url3 = urlparse.parse_qs(url2.query)
    parametervalue =  [key for key, key in url3.iteritems()] #[['aaa']]
    parsed =  parametervalue.append(xss[0])
    print parsed
    finalurl = urljoin(url, parsed)
    print finalurl





except Exception as e:
    print e

所以当我通过这个

xss3.py -t http://januapp.com/demo/search.php?search=aaa

错误发生在cmd

下面
ParseResult(scheme='http', netloc='januapp.com', path='/demo/search.php', params='', query='search=aaa', fragment='')
None
name 'urljoin' is not defined

请参阅None

现在就是问题,

我正在使用Python2.7。

非常感谢你。希望你能解决问题。

2 个答案:

答案 0 :(得分:1)

怎么样:

ext = "bbb"

a = "http://januapp.com/demo/search.php?search="

print a+ext

ext是您要搜索的内容,a是链接,只需将它们添加到一起即可。

或者你可以替换这样的值:

ext = "bbb"

a = "http://januapp.com/demo/search.php?search=aaa"

print a.replace('aaa', ext)

使用正则表达式:

import re

ext = "bbb"

a = "http://januapp.com/demo/search.php?search=aaa"

b=re.compile(r".+search=")

print re.search(b,a).group()+ext

答案 1 :(得分:1)

你可以尝试这种方法。

url = 'http://januapp.com/demo/search.php?search=aaa'

# First get all your query params
arr = url.split('?')
base_url = arr[0] # This is your base url i.e. 'http://januapp.com/demo/search.php'
params = arr[1] # here are your query params ['search=aaa']

# Now seprate out all the query parameters and their values
arr2 = params.split("=") # This will give you somrthing like this : ['search', 'aaa'], the the value will be next to the key

# This is a dictonary to hold the key value pairs
param_value_dict = {} # {'search': 'aaa'}
for i, str in enumerate(arr2):
    if i % 2 == 0:
        param_value_dict[str] = arr2[i + 1]



# now if you want to chnage the value of search from 'aaa' to 'bbb', then just change it in the dictonary
param_value_dict['search'] = 'bbb'

# now form the new url from the dictonary
new_url = base_url + '?'
for param_name, param_value in param_value_dict.items():
    new_url = new_url + param_name + "=" + param_value + "&"

# remove the extra '&'
new_url = new_url[:len(new_url) - 1]
print(new_url)