有些子节点名字直接就是编码后的url,就像下面这行一样:

url='dubbo%3A%2F%2F10.4.5.3%3A20880%2Fcom.welab.authority.service.AuthorityService%3Fanyhost%3Dtrue%26application%3Dwelab-authority%26dubbo%3D2.5.7'

先需要把这个url里进行解码, 转成如下这种:

dubbo://10.4.5.3:20880/com.welab.authority.service.AuthorityService?anyhost=true&application=welab-authority&dubbo=2.5.7

然后有用的参数: url中的ip地址, 参数中application对应的值

Python2解码与提取参数

>>> import urlparse
>>> urldata = urlparse.unquote(url)
>>> print urldata
dubbo://10.4.5.3:20880/com.welab.authority.service.AuthorityService?anyhost=true&application=welab-authority&dubbo=2.5.7

#转成一个格式化对象
>>> result = urlparse.urlparse(urldata)
>>> print result
ParseResult(scheme='dubbo', netloc='10.4.5.3:20880', path='/com.welab.authority.service.AuthorityService', params='', query='anyhost=true&application=welab-authority&dubbo=2.5.7', fragment='')

#提取url中的参数,即url中?后的内容
>>> args = urlparse.parse_qs(result.query)          
>>> print args
{'application': ['welab-authority'], 'anyhost': ['true'], 'dubbo': ['2.5.7']}

#获取我想要的信息  
>>> ip= result.netloc
>>> application = args.get('application',[])
>>> print ip
10.4.5.3:20880  
>>> print application
['welab-authority']

Python3 解码与提取参数

上面的测试中详细打印了输出信息, 这次就直接写在py脚本里了。

from urllib import parse

#url解码
urldata = parse.unquote(urldata)

#url结果  
result = parse.urlparse(urldata)

#url里的查询参数  
query_dict = parse.parse_qs(result.query)

#获取我需要的信息
ip = result.netloc  
application = query_dict.get('application',[])