前面说过,urllib库里还提供了parse
这个模块,它定义了处理URL的标准接口,例如实现URL各部分的抽取、合并以及连接转换。它支持以下协议的URL处理:file、ftp、gopher、hdl、http、https、imap、mailto、 mms、news、nntp、prospero、rsync、rtsp、rtspu、sftp、 sip、sips、snews、svn、svn+ssh、telnet和wais。本节中,咱们介绍一下该模块中经常使用的方法来看一下它的便捷之处。php
1. urlparse()
该方法能够实现URL的识别和分段,这里先用一个实例来看一下:html
1
2
3
4
|
from urllib.parse import urlparse
result = urlparse('http://www.baidu.com/index.html;user?id=5#comment')
print(type(result), result)
|
这里咱们利用urlparse()
方法进行了一个URL的解析。首先,输出了解析结果的类型,而后将结果也输出出来。git
运行结果以下:github
1
2
|
<class 'urllib.parse.ParseResult'>
ParseResult(scheme='http', netloc='www.baidu.com', path='/index.html', params='user', query='id=5', fragment='comment')
|
能够看到,返回结果是一个ParseResult
类型的对象,它包含6部分,分别是scheme
、netloc
、path
、params
、query
和fragment
。数据结构
观察一下该实例的URL:app
1
|
http://www.baidu.com/index.html;user?id=5#comment
|
能够发现,urlparse()
方法将其拆分红了6部分。大致观察能够发现,解析时有特定的分隔符。好比,://前面的就是scheme
,表明协议;第一个/前面即是netloc
,即域名;分号;前面是params
,表明参数。ssh
因此,能够得出一个标准的连接格式,具体以下:svn
1
|
scheme://netloc/path;parameters?query#fragment
|
一个标准的URL都会符合这个规则,利用urlparse()
方法能够将它拆分开来。ui
除了这种最基本的解析方式外,urlparse()
方法还有其余配置吗?接下来,看一下它的API用法:编码
1
|
urllib.parse.urlparse(urlstring, scheme='', allow_fragments=True)
|
能够看到,它有3个参数。
urlstring
:这是必填项,即待解析的URL。scheme
:它是默认的协议(好比http
或https
等)。假如这个连接没有带协议信息,会将这个做为默认的协议。咱们用实例来看一下:
1
2
3
4
|
from urllib.parse import urlparse
result = urlparse('www.baidu.com/index.html;user?id=5#comment', scheme='https')
print(result)
|
运行结果以下:
1
|
ParseResult(scheme='https', netloc='', path='www.baidu.com/index.html', params='user', query='id=5', fragment='comment')
|
能够发现,咱们提供的URL没有包含最前面的scheme
信息,可是经过指定默认的scheme
参数,返回的结果是https
。
假设咱们带上了scheme
:
1
|
result = urlparse('http://www.baidu.com/index.html;user?id=5#comment', scheme='https')
|
则结果以下:
1
|
ParseResult(scheme='http', netloc='www.baidu.com', path='/index.html', params='user', query='id=5', fragment='comment')
|
可见,scheme
参数只有在URL中不包含scheme
信息时才生效。若是URL中有scheme
信息,就会返回解析出的scheme
。
allow_fragments
:便是否忽略fragment
。若是它被设置为False
,fragment
部分就会被忽略,它会被解析为path
、parameters
或者query
的一部分,而fragment
部分为空。下面咱们用实例来看一下:
1
2
3
4
|
from urllib.parse import urlparse
result = urlparse('http://www.baidu.com/index.html;user?id=5#comment', allow_fragments=False)
print(result)
|
运行结果以下:
1
|
ParseResult(scheme='http', netloc='www.baidu.com', path='/index.html', params='user', query='id=5#comment', fragment='')
|
假设URL中不包含params
和query
,咱们再经过实例看一下:
1
2
3
4
|
from urllib.parse import urlparse
result = urlparse('http://www.baidu.com/index.html#comment', allow_fragments=False)
print(result)
|
运行结果以下:
1
|
ParseResult(scheme='http', netloc='www.baidu.com', path='/index.html#comment', params='', query='', fragment='')
|
能够发现,当URL中不包含params
和query
时,fragment
便会被解析为path
的一部分。
返回结果ParseResult
其实是一个元组,咱们能够用索引顺序来获取,也能够用属性名获取。示例以下:
1
2
3
4
|
from urllib.parse import urlparse
result = urlparse('http://www.baidu.com/index.html#comment', allow_fragments=False)
print(result.scheme, result[0], result.netloc, result[1], sep='\n')
|
这里咱们分别用索引和属性名获取了scheme
和netloc
,其运行结果以下:
1
2
3
4
|
http
http
www.baidu.com
www.baidu.com
|
能够发现,两者的结果是一致的,两种方法均可以成功获取。
2. urlunparse()
有了urlparse()
,相应地就有了它的对立方法urlunparse()
。它接受的参数是一个可迭代对象,可是它的长度必须是6,不然会抛出参数数量不足或者过多的问题。先用一个实例看一下:
1
2
3
4
|
from urllib.parse import urlunparse
data = ['http', 'www.baidu.com', 'index.html', 'user', 'a=6', 'comment']
print(urlunparse(data))
|
这里参数data
用了列表类型。固然,你也能够用其余类型,好比元组或者特定的数据结构。
运行结果以下:
1
|
http://www.baidu.com/index.html;user?a=6#comment
|
这样咱们就成功实现了URL的构造。
3. urlsplit()
这个方法和urlparse()
方法很是类似,只不过它再也不单独解析params
这一部分,只返回5个结果。上面例子中的params
会合并到path
中。示例以下:
1
2
3
4
|
from urllib.parse import urlsplit
result = urlsplit('http://www.baidu.com/index.html;user?id=5#comment')
print(result)
|
运行结果以下:
1
|
SplitResult(scheme='http', netloc='www.baidu.com', path='/index.html;user', query='id=5', fragment='comment')
|
能够发现,返回结果是SplitResult
,它其实也是一个元组类型,既能够用属性获取值,也能够用索引来获取。示例以下:
1
2
3
4
|
from urllib.parse import urlsplit
result = urlsplit('http://www.baidu.com/index.html;user?id=5#comment')
print(result.scheme, result[0])
|
运行结果以下:
1
|
http http
|
4. urlunsplit()
与urlunparse()
相似,它也是将连接各个部分组合成完整连接的方法,传入的参数也是一个可迭代对象,例如列表、元组等,惟一的区别是长度必须为5。示例以下:
1
2
3
4
|
from urllib.parse import urlunsplit
data = ['http', 'www.baidu.com', 'index.html', 'a=6', 'comment']
print(urlunsplit(data))
|
运行结果以下:
1
|
http://www.baidu.com/index.html?a=6#comment
|
5. urljoin()
有了urlunparse()
和urlunsplit()
方法,咱们能够完成连接的合并,不过前提必需要有特定长度的对象,连接的每一部分都要清晰分开。
此外,生成连接还有另外一个方法,那就是urljoin()
方法。咱们能够提供一个base_url
(基础连接)做为第一个参数,将新的连接做为第二个参数,该方法会分析base_url
的scheme
、netloc
和path
这3个内容并对新连接缺失的部分进行补充,最后返回结果。
下面经过几个实例看一下:
1
2
3
4
5
6
7
8
9
10
|
from urllib.parse import urljoin
print(urljoin('http://www.baidu.com', 'FAQ.html'))
print(urljoin('http://www.baidu.com', 'https://cuiqingcai.com/FAQ.html'))
print(urljoin('http://www.baidu.com/about.html', 'https://cuiqingcai.com/FAQ.html'))
print(urljoin('http://www.baidu.com/about.html', 'https://cuiqingcai.com/FAQ.html?question=2'))
print(urljoin('http://www.baidu.com?wd=abc', 'https://cuiqingcai.com/index.php'))
print(urljoin('http://www.baidu.com', '?category=2#comment'))
print(urljoin('www.baidu.com', '?category=2#comment'))
print(urljoin('www.baidu.com#comment', '?category=2'))
|
运行结果以下:
1
2
3
4
5
6
7
8
|
http://www.baidu.com/FAQ.html
https://cuiqingcai.com/FAQ.html
https://cuiqingcai.com/FAQ.html
https://cuiqingcai.com/FAQ.html?question=2
https://cuiqingcai.com/index.php
http://www.baidu.com?category=2#comment
www.baidu.com?category=2#comment
www.baidu.com?category=2
|
能够发现,base_url
提供了三项内容scheme
、netloc
和path
。若是这3项在新的连接里不存在,就予以补充;若是新的连接存在,就使用新的连接的部分。而base_url
中的params
、query
和fragment
是不起做用的。
经过urljoin()
方法,咱们能够轻松实现连接的解析、拼合与生成。
6. urlencode()
这里咱们再介绍一个经常使用的方法——urlencode()
,它在构造GET请求参数的时候很是有用,示例以下:
1
2
3
4
5
6
7
8
9
|
from urllib.parse import urlencode
params = {
'name': 'germey',
'age': 22
}
base_url = 'http://www.baidu.com?'
url = base_url + urlencode(params)
print(url)
|
这里首先声明了一个字典来将参数表示出来,而后调用urlencode()
方法将其序列化为GET请求参数。
运行结果以下:
1
|
http://www.baidu.com?name=germey&age=22
|
能够看到,参数就成功地由字典类型转化为GET请求参数了。
这个方法很是经常使用。有时为了更加方便地构造参数,咱们会事先用字典来表示。要转化为URL的参数时,只须要调用该方法便可。
7. parse_qs()
有了序列化,必然就有反序列化。若是咱们有一串GET请求参数,利用parse_qs()
方法,就能够将它转回字典,示例以下:
1
2
3
4
|
from urllib.parse import parse_qs
query = 'name=germey&age=22'
print(parse_qs(query))
|
运行结果以下:
1
|
{'name': ['germey'], 'age': ['22']}
|
能够看到,这样就成功转回为字典类型了。
8. parse_qsl()
另外,还有一个parse_qsl()
方法,它用于将参数转化为元组组成的列表,示例以下:
1
2
3
4
|
from urllib.parse import parse_qsl
query = 'name=germey&age=22'
print(parse_qsl(query))
|
运行结果以下:
1
|
[('name', 'germey'), ('age', '22')]
|
能够看到,运行结果是一个列表,而列表中的每个元素都是一个元组,元组的第一个内容是参数名,第二个内容是参数值。
9. quote()
该方法能够将内容转化为URL编码的格式。URL中带有中文参数时,有时可能会致使乱码的问题,此时用这个方法能够将中文字符转化为URL编码,示例以下:
1
2
3
4
5
|
from urllib.parse import quote
keyword = '壁纸'
url = 'https://www.baidu.com/s?wd=' + quote(keyword)
print(url)
|
这里咱们声明了一个中文的搜索文字,而后用quote()
方法对其进行URL编码,最后获得的结果以下:
1
|
https://www.baidu.com/s?wd=%E5%A3%81%E7%BA%B8
|
10. unquote()
有了quote()
方法,固然还有unquote()
方法,它能够进行URL解码,示例以下:
1
2
3
4
|
from urllib.parse import unquote
url = 'https://www.baidu.com/s?wd=%E5%A3%81%E7%BA%B8'
print(unquote(url))
|
这是上面获得的URL编码后的结果,这里利用unquote()
方法还原,结果以下:
1
|
https://www.baidu.com/s?wd=壁纸
|
能够看到,利用unquote()
方法能够方便地实现解码。
本节中,咱们介绍了parse
模块的一些经常使用URL处理方法。有了这些方法,咱们能够方便地实现URL的解析和构造,建议熟练掌握。