1、完整的url语法格式:
协议://用户名@密码:子域名.域名.顶级域名:端口号/目录/文件名.文件后缀?参数=值#标识
2、urlparse模块对url的处理方法
# -*- coding:utf-8 -*-
import urlparse
# 利用urlparse()方法对url进行解析,返回六元组;urlunparse()对六元组进行组合。
url = "http://username@password:www.baidu.com/s?name=liu&age=12"
res = urlparse.urlparse(url)
print res, type(res) # <class 'urlparse.ParseResult'>即元组
res = urlparse.urlunparse(res)
print res, type(res) # <type 'str'>
# # 利用urljoin()方法对绝对url地址与相对url地址进行拼合
url = ("http://www.baidu.com", "index.html")
url = urlparse.urljoin(url[0], url[1])
print url # http://www.baidu.com/index.html
# 利用urlsplit()方法可以对URL进行分解;与urlparse()相比,urlsplit()函数返回一个五元组,没有parameter参数。
url = "http://username@password:www.baidu.com/s?name=liu&age=12"
res = urlparse.urlsplit(url)
print res, type(res) # <class 'urlparse.SplitResult'>即元组
res = urlparse.urlunsplit(res)
print res, type(res) # <type 'str'>
3、urllib模块对url的编码与解码
# -*- coding:utf-8 -*-
import urllib
url = 'http://www.baidu.com/s'
values = {"name": "liu", "age": 12, "love": "游戏"}
params = urllib.urlencode(values)
print params, type(params) # age=12&love=%E6%B8%B8%E6%88%8F&name=liu <type 'str'>
result = urllib.unquote(params)
print result, type(result) # age=12&love=游戏&name=liu <type 'str'>
print url + '?' + params # http://www.baidu.com/s?age=12&love=%E6%B8%B8%E6%88%8F&name=liu
a = urllib.quote("游戏")
b = urllib.unquote(a)
print a, type(a) # %E6%B8%B8%E6%88%8F <type 'str'>
print b, type(b) # 游戏 <type 'str'>
# 参考:https://blog.csdn.net/haoni123321/article/details/15814111/
# 参考:https://blog.csdn.net/vinson0526/article/details/51745491
# 参考:https://blog.csdn.net/tycoon1988/article/details/40514555
# 参考:https://www.jb51.net/article/92818.htm