簡體   English   中英

如何在python中更改參數的值?

[英]How to change the value of the parameter in python?

注意:沒有修復URL。 意味着不可能總是看到該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。

非常感謝你。 希望你能解決這個問題。

怎么樣:

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

您可以嘗試使用這種方法。

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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM