python - 如何改变python中参数的值?

标签 python python-2.7

注意:它没有固定网址。意味着不可能总是看到这个网址。我想要适用于所有网址的代码。

例如,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

查看

这就是问题所在

我使用的是Python2.7。

非常感谢。希望您能解决这个问题。

最佳答案

你可以尝试用这种方法。

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)

关于python - 如何改变python中参数的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49108465/

相关文章:

django - 记录列表未在 Django REST 框架中获取更新的记录..?

python-2.7 - 如何使用 azure cosmos db 最大化数据库上传速率

python - Google App Engine - 一个项目中的多个 yaml 文件共享一个库

python - 仅参数作为值的 case 表达式中的数据类型未知

python - Python 中 R 的 qqplot 的等价物

python - 在 Python 中打印具有多个值的类

java - Python 到 Java 代码转换

python - 以图像作为标签的 Caffe 测试网

python - 如何让 Kivy Accordion 在按下时折叠以及如何设置默认 Accordion 折叠设置

python - 追加带有 pandas' to_hdf 的行是 H5 文件大小的倍数吗?