简体   繁体   中英

How to change the value of the parameter in python?

NOTE: There is no fix url for it. Means it is not possible to see this url always. I want code which works for all the urls.

For ex, http://januapp.com/demo/search.php?search=aaa
http://januapp.com/demo/search.php?other=aaa

Now I want to change it to

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

I don't know how can I do it?

I tried this

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

So when I pass this

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

The Error occurs below on to the cmd

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

See the None

Now that's the problem,

I am using Python2.7.

Thank you very much. Hope you get the problem.

How about:

ext = "bbb"

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

print a+ext

Where ext is what you want to search for, a is the link and just add them together.

Or you could replace values like this:

ext = "bbb"

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

print a.replace('aaa', ext)

Using regex:

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

You can try something with this kind of approach.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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