立即学习:https://edu.csdn.net/course/play/24797/282208?utm_source=blogtoedu
1.urllib.parse:用于解析URL
2.使用urlparse()函数来解析字符串
3.使用urlunparse()函数则可以把一个ParseResult对象或元组恢复成URL自促穿
4.parse_qs()、parse_qsl() 用于解析查询字符串得到字典或列表
5.urlencode():将列表或字典恢复成查询字符串
import urllib.parse as up
url = 'https://blog.csdn.net/happyk213'
# uv = up.urlparse(url)
# print(uv)
# print('scheme:', uv.scheme)
# print('netloc:', uv.netloc)
# print('path:', uv.path)
# print('params:', uv.params)
# print('query:', uv.query)
# print('fragment', uv.fragment)
#
# uv2 = up.urlunparse(uv)
# print(uv2)
str = 'name=123&name=abc&age=18&sex=1&age=1&height=190'
strf = up.parse_qs(str)
print(strf)
strf = up.parse_qsl(str)
print(strf)
strpq = {'name': ['123', 'abc'], 'age': ['18', '1'], 'sex': ['1'], 'height': ['190']}
strpql = [('name', '123'), ('name', 'abc'), ('age', '18'), ('sex', '1'), ('age', '1'), ('height', '190')]
#编码转换
print(up.quote('无聊猿的一日'))
#解码
print(up.unquote('%E6%97%A0%E8%81%8A%E7%8C%BF%E7%9A%84%E4%B8%80%E6%97%A5'))
print(up.urlencode(strpq))
print(up.unquote(up.urlencode(strpq)))
print(up.urlencode(strpql))