解析链接
1. urlparse() & urlunparse()
urlparse()
是对url链接识别和分段的,API用法如下:
urllib.parse.urlparse(urlstring, scheme='', allow_fragments=True)
他的三个参数:
- urlstring: 这是一个必须项,即待解析的url。
- scheme: 它是默认协议。假如这个链接没有带协议信息,会将这个作为默认协议。
from urllib.parse import urlparse
result = urlparse('www.baidu.com/index.html;user?id=5#comment', scheme='https')
print(result)
结果为:
ParseResult(scheme='http', netloc='www.baidu.com', path='/index.html', params='user', query='id=5', fragment='comment')
allow_fragments
: 即是否忽略fragment
。如果他被设置为False
,fragment
部分就会被忽略,他会解析为path
,parameters
或者query
的一部分,而fragment
部分为空
from urllib.parse import urlparse
result = urlparse('http://www.baidu.com/index.html;user?id=5#comment', allow_fragments=False)
print(result)
结果如下:
ParseResult(scheme='http', netloc='www.baidu.com', path='/index.html', params='user', query='id=5#comment', fragment='')
假设URL中不包含params
和query
,如下:
from urllib.parse import urlparse
result = urlparse('http://www.baidu.com/index.html#comment', allow_fragments=False)
print(result)
运行结果如下:
ParseResult(scheme='http', netloc='www.baidu.com', path='/index.html#comment', params='', query='', fragment='')
可以发现,当URL中不包含params
和query
时,fragment
便会被解析为path的一部分。返回结果ParseResult
实际上是一个元组,我们可以用索引顺去来获取,也可以用属性名获取。如下:
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')
结果如下:
http
http
www.baidu.com
www.baidu.com
可以发现二者结果一致
urlunparse()
是其对立方法,他可接收一个可迭代对象。但是长度必须是6,否则会抛出参数数量不足或者过多的问题。如下:
from urllib.parse import urlunparse
data = ['http', 'www.baidu.com', 'index.html', 'user', 'a=6', 'comment']
print(urlunparse(data))
结果如下:
http://www.baidu.com/index.html;user?a=6#comment
2. urlsplite() & urlunsplite()
unsplite()
和urlparse()
方法相似。但是不能单独解析params这一部分,只返回5个结果。
from urllib.parse import urlsplit
result = urlsplit('http://www.baidu.com/index.html;user?id=5#comment')
print(result)
结果:
SplitResult(scheme='http', netloc='www.baidu.com', path='/index.html;user', query='id=5', fragment='comment')
返回结果SplitResult
是一个元组类型,既可以用属性获取值,也可以用索引来获取。如下:
from urllib.parse import urlsplit
result = urlsplit('http://www.baidu.com/index.html;user?id=5#comment')
print(result.scheme, result[0])
结果:
http http
urlunsplit()
和urlunparse()
方法类似,他也是将链接各个部分组合成完整链接的方法。传入的参数也是一个可迭代对象。长度必须为5,如下:
from urllib.parse import urlunsplit
data = ['http', 'www.baidu.com', 'index.html', 'a=6', 'comment']
print(urlunsplit(data))
结果:
http://www.baidu.com/index.html?a=6#comment
3. urljoin()
生成链接的另外一个方法,可以提供一个base_url
作为第一个参数,将新的链接作为第二个参数,该方法会分析base_url
的scheme
、netloc
和path
这3个内容并对链接缺失部分进行补充。如下:
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'))
结果如下:
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
是不起作用的。
4. urlencode()
urlencode()
方法在构造GET请求参数的时候非常有用,如下:
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请求参数。
http://www.baidu.com?name=germey&age=22
5. parse_qs()
这是一个反序列化方法,我们可以利用parse_qs()
方法,就可以转回字典,如下:
from urllib.parse import parse_qs
query = 'name=germey&age=22'
print(parse_qs(query))
结果
{'name': ['germey'], 'age': ['22']}
6. parse_qsl()
将参数转化成元组列表
from urllib.parse import parse_qsl
query = 'name=germey&age=22'
print(parse_qsl(query))
结果:
[('name', 'germey'), ('age', '22')]
7. quote() & unquote()
quote()
是将中文转换成URL编码,unquote()
是对URL进行解码。
from urllib.parse import quote
keyword = '壁纸'
url = 'https://www.baidu.com/s?wd=' + quote(keyword)
print(url)
https://www.baidu.com/s?wd=%E5%A3%81%E7%BA%B8
from urllib.parse import unquote
url = 'https://www.baidu.com/s?wd=%E5%A3%81%E7%BA%B8'
print(unquote(url))
https://www.baidu.com/s?wd=壁纸
参考崔庆才博客