python 版本3.6
1.urlparse
定义:def urlparse(url, scheme=”, allow_fragments=True)
将url拆分成六大组件
#!/usr/bin/evn python
"测试urlparse"
#导入parse模块
from urllib import parse
urp = parse.urlparse('http://www.baidu.com:80/doc?age=5#ff')
print(urp)
#result:
ParseResult(scheme='http', netloc='www.baidu.com:80', path='/doc', params='', query='age=5', fragment='ff')
print(urp.scheme)
#result:
http
疑问:params是干什么的?有什么用?怎么才能有了值呢?
2.urlunparse
定义:def urlunparse(components)
功能:将拆分的组件合并为url
#!/usr/bin/evn python
"测试urlunparse"
#导入parse模块
from urllib import parse
pr = parse.ParseResult(scheme='http', netloc='www.baidu.com:80', path='/doc', params='fd;ff', query='age=5', fragment='ff')
print(parse.urlunparse(pr))
#result:
http://www.baidu.com:80/doc;paramsstr?age=5#ff
疑问解答:
更在path后面的;str就是params ,但是是干什么用的呢?
#!/usr/bin/evn python
"测试urlunparse"
#导入parse模块
from urllib import parse
sr = parse.urlsplit('http://www.baidu.com:80/doc?age=5#ff')
print(sr)
#result
SplitResult(scheme='http', netloc='www.baidu.com:80', path='/doc', query='age=5', fragment='ff')
#!/usr/bin/evn python
"测试urlunparse"
#导入parse模块
from urllib import parse
sr = parse.SplitResult(scheme='http', netloc='www.baidu.com:80', path='/doc', query='age=5', fragment='ff')
print(parse.urlunsplit(sr))
#result
http://www.baidu.com:80/doc?age=5#ff
5.urljoin
定义:def urljoin(base, url, allow_fragments=True)
如果url是完整的url路径直接返回
#!/usr/bin/evn python
"测试urlunparse"
#导入parse模块
from urllib import parse
#相对路径
parse.urljoin('http://www.baidu.com/doc/article.html','excel/1')
# result: http://www.baidu.com/doc/excel/1
parse.urljoin('http://www.baidu.com/doc/article.html','../excel/1')
# result: http://www.baidu.com/excel/1
#绝对路径
parse.urljoin('http://www.baidu.com/doc/abc/article.html','/excel/1')
#result: http://www.baidu.com/excel/1
6.urldefrag
定义:def urldefrag(url)
功能:把#后边的东西和前面的分开 (个人理解)
#!/usr/bin/evn python
"测试urlunparse"
#导入parse模块
from urllib import parse
ud = parse.urldefrag('http://www.baidu.com/doc/article.html#doc')
print(ud)
result:
DefragResult(url='http://www.baidu.com/doc/article.html', fragment='doc')
7.quote |quote_from_bytes
定义:quote(string, safe=’/’, encoding=None, errors=None)
功能:转码特殊字符,准确的说叫保留字符,下面已列出
“;” | “/” | “?” | “:” | “@” | “&” | “=” | “+” |”$” | “,”
#!/usr/bin/evn python
"测试urlunparse"
#导入parse模块
from urllib import parse
print(parse.quote('http://www.baidu.com/doc/sub.html?name=han jian&age=45@;+$'))
#result:http%3A//www.baidu.com/doc/sub.html%3Fname%3Dhan%20jian%26age%3D45%40%3B%2B%24
#safe 将不被转码
print(parse.quote('http://www.baidu.com/doc/sub.html?name=han jian&age=45@;+$','/+$'))
#result:http%3A//www.baidu.com/doc/sub.html%3Fname%3Dhan%20jian%26age%3D45%40%3B+$
8.quote_plus,非常类似于quote只不过是将空格转码为+
9.unquote|unquote_to_bytes
定义:def unquote(string, encoding=’utf-8’, errors=’replace’)
功能:quote的逆过程
#!/usr/bin/evn python
"测试urlunparse"
#导入parse模块
from urllib import parse
print(parse.unquote('http%3A//www.baidu.com/doc/sub.html%3Fname%3Dhan%20jian%26age%3D45%40%3B+$'))
#result:http://www.baidu.com/doc/sub.html?name=han jian&age=45@;+$
10.unquote_plus
定义:def unquote_plus(string, encoding=’utf-8’, errors=’replace’)
功能:quote_plus的逆过程
11.urlencode
定义:urlencode(query, doseq=False, safe=”, encoding=None, errors=None,quote_via=quote_plus)
功能:将字典形式的数据转化成查询字符串
参数的含义:
query:需要转化的字典数据
doseq:如果字典的某个值是序列的话是否解析,deseq值为False不解析doseq的值为True的时候解析,稍后在例子中给出
safe:那些字符串不需要编码
encoding:要转化成的字符串的编码
quote_via:使用quote编码还是qutoe_plus编码,默认quote_plus也就是空格被转化成+号
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
"urlencode 测试"
from urllib import parse
#定义要转化的字典数据
qdict = {'age':34,'grils':('lili','tingting'),'name':'han p$'}
print(parse.urlencode(qdict))
#result:
#age=34&grils=%28%27lili%27%2C+%27tingting%27%29&name=han+p%24
#怎么让两个女朋友分开呢
print(parse.urlencode(qdict,True))
#result
#age=34&grils=lili&grils=tingting&name=han+p%24
#怎么让name里边的$不要编码呢
print(parse.urlencode(qdict,True,'$'))
#result
age=34&grils=lili&grils=tingting&name=han+p$
#怎么让空格不编码成+而编译成%20呢
print(parse.urlencode(qdict,True,'$',quote_via=parse.quote))
#由于前面还有两个位置参数所以使用关键字参数
#result
age=34&grils=lili&grils=tingting&name=han%20p$
12.
定义: unwrap(url):
“”“unwrap(‘
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
"splittype 测试"
from urllib import parse
print(parse.splithost('//www.baidu.com:90/doc/h.html'))
#result:
#('http', '//wwww.baidu.com')
14.splithost
定义:splithost(url)
功能:将协议名和后面的网址分开
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
"splithost测试"
from urllib import parse
print(parse.splithost('//www.baidu.com:90/doc/h.html'))
#为什么要要求//开头呢?我估计是可以用splittype的结果
#result:
#('www.baidu.com:90', '/doc/h.html')
15.splituser
定义:splituser(url)
功能:用户信息和网址信息分开
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
"splituser 测试"
from urllib import parse
print(parse.splituser ('han.ji:[email protected]'))
#result:
#('han.ji:dd', '163.com')
#可以见得如果想将用户名和密码分开还的splitpasswd
16.splitpasswd
定义:splitpasswd
功能:将用户名和密码分开
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
"splitpassword 测试"
from urllib import parse
print(parse.splitpasswd('han.ji:dd'))
#result:
#('han.ji', 'dd')
17,splitport
定义:splitport(host)
功能:将网址和端口号分开,这个有个要求端口号后面不能有任何东西
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
"splitport测试"
from urllib import parse
print(parse.splitport('http://www.baidu.com:80'))
#result:
#('http://www.baidu.com', '80')
18.splitquery
定义:splitquery(url)
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
"splitquery测试"
from urllib import parse
print(parse.splitquery('http://www.baidu.com:80?dd=88'))
#result:
#('http://www.baidu.com:80', 'dd=88')
19.splittag
def splittag(url):
"""splittag('/path#tag') --> '/path', 'tag'."""
path, delim, tag = url.rpartition('#')
if delim:
return path, tag
return url, None
功能:用#切割字符串
20.splitattr
定义
def splitattr(url):
"""splitattr('/path;attr1=value1;attr2=value2;...') ->
'/path', ['attr1=value1', 'attr2=value2', ...]."""
words = url.split(';')
return words[0], words[1:]
21.
定义:
def splitvalue(attr):
"""splitvalue('attr=value') --> 'attr', 'value'."""
attr, delim, value = attr.partition('=')
return attr, (value if delim else None)
功能:切割键和值
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
"splitvalue测试"
from urllib import parse
print(parse.splitvalue('dd=89'))
#result:
#('dd', '89')