-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathurl解析链接.py
77 lines (66 loc) · 2.49 KB
/
url解析链接.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#解析链接
#1.urlparse()
# from urllib.parse import urlparse
# result=urlparse('http://www.baidu.com/index.html;user?id=5#comment')
# print(result)
# print(result.scheme)
# print(result[0])
# print(result.netloc)
# print(result[1])
# from urllib.parse import urlparse
# result=urlparse('www.baidu.com/index.html;user?id=5#comment',scheme='https')
# print(result)
# from urllib.parse import urlparse
# result=urlparse('www.baidu.com/index.html#comment',scheme='https',allow_fragments=False)
# print(result)
# #2.urlunparse
# from urllib.parse import urlunparse
# data=('https','www.baidu.com','index','user','id=5','comment') #可用列表、元组或其他数据结构,长度必须为6
# print(urlunparse(data))
#3.urlsplit
# from urllib.parse import urlsplit
# result=urlsplit('http://www.baidu.com/index.html;user?id=5#comment')
# print(result)
# print(result.scheme)
# print(result[0])
# #4.urllibunsplit
# from urllib.parse import urlunsplit
# data=['http','www.baidu.com','index.html','id=5','comment'] #可用列表、元组或其他数据结构,长度必须为5
# result=urlunsplit(data)
# print(result)
# #5.urljoin 只要基础链接的scheme,netloc,path,若新链存在,则使用新链
# from urllib.parse import urljoin
# print(urljoin('http://www.baidu.com','index.html'))
# print(urljoin('http://www.baidu.com','http://www.qianfg123.cn/2.html'))
# print(urljoin('http://www.baidu.com/about.html','http://www.qianfg123.cn/2.html'))
# print(urljoin('http://www.baidu.com/about.html','http://www.qianfg123.cn/2.html?id=5'))
# print(urljoin('http://www.baidu.com?wd=5','http://www.qianfg123.cn/2.html'))
# print(urljoin('www.baidu.com','?id=5#comment'))
# print(urljoin('www.baidu.com#comment','?id=5'))
# #6.urlencode
# from urllib.parse import urlencode
# params={
# 'name':'qianfg',
# 'age':22
# }
# base_url='http://www.baidu.com?'
# url=base_url+urlencode(params)
# print(url)
# #7.parse_qs 反序列化,转为字典
# from urllib.parse import parse_qs
# url='name=qianfg&age=22'
# print(parse_qs(url))
# #8.parse_qsl 反序列化,转为元组组成的列表
# from urllib.parse import parse_qsl
# url='name=qianfg&age=22'
# print(parse_qsl(url))
# #9.quote 把内容转化为URL编码格式
# from urllib.parse import quote
# keyword='壁纸'
# url='http://www.baidu.com?wd='+quote(keyword)
# print(url)
#
# #10.unquote
# from urllib.parse import unquote
# url='http://www.baidu.com?wd=%E5%A3%81%E7%BA%B8'
# print(unquote(url))