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
| """ urllib.parse 解析url """ from urllib.parse import *
result = urlparse('http://www.crazy.org:80/index.php;yeeku?name=fkit#frag')
print(result)
print('scheme:', result.scheme, result[0]) print('主机和端口:', result.netloc, result[1]) print('主机:', result.hostname) print('端口:', result.port) print('资源路径:', result.path, result[2]) print('参数:', result.params, result[3]) print('查询字符串:', result.query, result[4]) print('fragment:', result.fragment, result[5]) print(result.geturl())
print('------urlunparse-----') result = urlunparse(('http', 'www.crazy.org:80', 'index.php', 'yeeku', 'name=fkit', 'frag')) print(result)
result = urlparse('//www.crazyit.org:80/index.php') print('scheme:', result.scheme, result[0]) print('主机和端口:', result.netloc, result[1]) print('资源路径', result.path, result[2])
print('------------------------------') result = urlparse('www.crazyit.org/index.php') print('scheme:', result.scheme, result[0]) print('主机和端口:', result.netloc, result[1]) print('资源路径', result.path, result[2])
print('---------------------')
result = parse_qs('name=fkit&name=%E6%98%8E%E5%A4%A9&age=12') print(result)
result = parse_qsl('name=fkit&name=%E6%98%8E%E5%A4%A9&age=12') print(result)
print(urlencode(result))
print('------------urljoin--------------')
result = urljoin('http://www.crazyit.org/users/login.html', 'help.html') print(result)
result = urljoin('http://www.crazyit.org/users/login.html', '/help.html') print(result)
result = urljoin('http://www.crazyit.org/users/login.html', '//help.html') print(result)
|