1、获取url参数。
>>> from urllib import parse >>> url = r‘https://docs.python.org/3.5/search.html?q=parse&check_keywords=yes&area=default‘ >>> parseResult = parse.urlparse(url) >>> parseResult ParseResult(scheme=‘https‘, netloc=‘docs.python.org‘, path=‘/3.5/search.html‘, params=‘‘, query=‘q=parse&check_keywords=yes&area=default‘, fragment=‘‘) >>> param_dict = parse.parse_qs(parseResult.query) >>> param_dict {‘q‘: [‘parse‘], ‘check_keywords‘: [‘yes‘], ‘area‘: [‘default‘]} >>> q = param_dict[‘q‘][0] >>> q ‘parse‘
>>> from urllib import parse >>> query = { ‘name‘: ‘walker‘, ‘age‘: 99, } >>> parse.urlencode(query) ‘name=walker&age=99‘
>>> from urllib import parse >>> parse.quote(‘a&b/c‘) #未编码斜线 ‘a%26b/c‘ >>> parse.quote_plus(‘a&b/c‘) #编码了斜线 ‘a%26b%2Fc‘
from urllib import parse >>> parse.unquote(‘1+2‘) #不解码加号 ‘a&b/c‘ >>> parse.unquote(‘1+2‘) #把加号解码为空格 ‘1 2‘
如果你还想问为什么没有urldecode——再把示例1看五遍。^_^
相关阅读:urllib.parse — Parse URLs into components
*** walker ***
时间: 2024-10-21 00:03:34