1. 拆分url
1 from urllib.parse import urlparse 2 3 result = urlparse("http://www.baidu.com/index.html;user?id=5#comment") 4 print(result) 5 print(list(result)) 6 print(result[0]) 7 8 # 结果: 9 # ParseResult(scheme='http', netloc='www.baidu.com', path='/index.html', params='user', query='id=5', fragment='comment') 10 # ['http', 'www.baidu.com', '/index.html', 'user', 'id=5', 'comment'] 11 # http
2. 拼接url
1 from urllib.parse import urlunparse 2 3 data = ['http','www.baidu.com','index.html','user','a=123','commit'] 4 print(urlunparse(data)) 5 6 # 结果: 7 # http://www.baidu.com/index.html;user?a=123#commit 8 9 from urllib.parse import urljoin 10 11 # 另一个拼贴url 12 13 print(urljoin('http://www.baidu.com', 'FAQ.html')) 14 print(urljoin('http://www.baidu.com', 'https://pythonsite.com/FAQ.html')) 15 print(urljoin('http://www.baidu.com/about.html', 'https://pythonsite.com/FAQ.html')) 16 print(urljoin('http://www.baidu.com/about.html', 'https://pythonsite.com/FAQ.html?question=2')) 17 print(urljoin('http://www.baidu.com?wd=abc', 'https://pythonsite.com/index.php')) 18 print(urljoin('http://www.baidu.com', '?category=2#comment')) 19 print(urljoin('www.baidu.com', '?category=2#comment')) 20 print(urljoin('www.baidu.com#comment', '?category=2')) 21 22 # 结果: 23 # http://www.baidu.com/FAQ.html 24 # https://pythonsite.com/FAQ.html 25 # https://pythonsite.com/FAQ.html 26 # https://pythonsite.com/FAQ.html?question=2 27 # https://pythonsite.com/index.php 28 # http://www.baidu.com?category=2#comment 29 # www.baidu.com?category=2#comment 30 # www.baidu.com?category=2
3. 另一个url拼接
1 print(urljoin('http://www.baidu.com', 'FAQ.html')) 2 print(urljoin('http://www.baidu.com', 'https://pythonsite.com/FAQ.html')) 3 print(urljoin('http://www.baidu.com/about.html', 'https://pythonsite.com/FAQ.html')) 4 print(urljoin('http://www.baidu.com/about.html', 'https://pythonsite.com/FAQ.html?question=2')) 5 print(urljoin('http://www.baidu.com?wd=abc', 'https://pythonsite.com/index.php')) 6 print(urljoin('http://www.baidu.com', '?category=2#comment')) 7 print(urljoin('www.baidu.com', '?category=2#comment')) 8 print(urljoin('www.baidu.com#comment', '?category=2')) 9 10 # 结果: 11 # http://www.baidu.com/FAQ.html 12 # https://pythonsite.com/FAQ.html 13 # https://pythonsite.com/FAQ.html 14 # https://pythonsite.com/FAQ.html?question=2 15 # https://pythonsite.com/index.php 16 # http://www.baidu.com?category=2#comment 17 # www.baidu.com?category=2#comment 18 # www.baidu.com?category=2
4. 字典转为参数
1 from urllib.parse import urlencode 2 3 params = { 4 "name":"zhaofan", 5 "age":23, 6 } 7 base_url = "http://www.baidu.com?" 8 9 url = base_url+urlencode(params) 10 print(url) 11 12 # 结果 13 # http://www.baidu.com?name=zhaofan&age=23
5. 参数转为字典
1 url = '?id=1&name=xiaocai&' 2 3 from urllib.parse import parse_qs 4 5 url = url.split('?') 6 7 aa = parse_qs(url[1]) 8 print(aa) 9 10 # 结果: 11 {'id': ['1'], 'name': ['xiaocai']}