urllib庫:解析鏈接
阿新 • • 發佈:2019-01-07
letter esc cin adding code per urllib lan color
1from urllib.parse import urlparse, urlunparse, urlsplit, urlunsplit, urljoin, urlencode, parse_qs, parse_qsl, quote,unquote
1.urlparse() 解析url
1result = urlparse(‘https://mbd.baidu.com/newspage/data/landingsuper?context=%7B%22nid%22%3A%22news_9094304700265862330%22%7D&n_type=0&p_from=1‘)
2print(type(result), result)
3 print(result.scheme)
4print(result[0])
2.urlunparse() 構造url 長度最長為6
1data = [‘https‘, ‘www.baidu.com‘, ‘index.html‘, ‘user‘, ‘a=6‘, ‘comment‘]
2print(urlunparse(data))
3.urlsplit() 把參數合並到path
1result = urlsplit(‘https://www.baidu.com/index.html;user?a=6#comment‘)
2print(result)
3print(result.scheme)
4print(result[0])
4.urlunsplit() 組成完整的鏈接
1data = [‘https‘, ‘www.baidu.com‘, ‘index.html‘, ‘a=6‘, ‘comment‘]
2print(urlunsplit(data))
5.urljoin() 可以實現鏈接的解析,拼合,與生成
1print(urljoin("https://www.baidu.com/", ‘FAQ.html‘))
2print(urljoin(‘http://www.baiduu.com‘, ‘https://www.gxj.com/FAQ.html‘))
6.urlencode() 字典轉化為請求參數
1params = {
2 ‘name‘ : ‘germey‘,
3 ‘age‘: 22
4}
5baseurl = ‘http://www.baiduu.com?‘
6url = baseurl + urlencode(params)
7print(‘--urlencode--%s‘ %url)
7.parse_qs() 轉化為字典
1query = ‘name=germey&age=22‘
2print(‘--parse_qs---%s‘ %parse_qs(query))
8.parse_qsl() 參數轉化為元祖組成的列表
1query = ‘name=germey&age=22‘
2print(‘--parse_qsl--%s‘ %parse_qsl(query))
9.quote() 將中文轉化為英文
1keyword = ‘我愛你‘
2url = ‘http://www.baiduu.com‘ + quote(keyword)
3print(‘--quote()--%s‘%url)
10.unquote() 將英文轉化為中文
1keyword =‘%E6%88%91%E7%88%B1%E4%BD%A0‘
2url = ‘http://www.baiduu.com‘ + unquote(keyword)
3print(‘--quote()--%s‘%url)
urllib庫:解析鏈接