from urllib.parse import *
params = {
'name': 'germey',
'age': 22
}
base_url = 'http://www.baidu.com?'
urlencode + parse_qs
> urlencode(params) # 输出:'name=germey&age=22'
> parse_qs('name=germey&age=22') # 输出:{'name': ['germey'], 'age': ['22']}
> parse_qsl('name=germey&age=22') # 输出:[('name', 'germey'), ('age', '22')]
quote + unquote :
quote()将内容转化为URL编码的格式, 防止中文乱码。
> quote("高清") # 输出:'%E9%AB%98%E6%B8%85'
urlparse + urlunparse
解析URL得到6parts: <scheme>://<netloc>/<path>;<params>?<query>#<fragment>
> url = 'http://www.baidu.com/index.php?username=guo'
> urlparse(url)
Out: ParseResult(scheme='http', netloc='www.baidu.com', path='/index.php', params='', query='username=guo', fragment='')
> urlunparse(ret) # 从元祖构建url
urlsplit + urlunsplit
解析URL得到5parts: <scheme>://<netloc>/<path>?<query>#<fragment>
> ret= urlparse.urlsplit(url)
输出: SplitResult(scheme='http', netloc='www.baidu.com', path='/index.php', query='username=guo', fragment='')
> urlunsplit(ret)
urljoin
urljoin('http://www.oschina.com/tieba','index.php') # 'http://www.oschina.com/index.php' base不是以字符/结尾时,
urljoin('http://www.oschina.com/tieba/','index.php') # 'http://www.oschina.com/tieba/index.php'