Skip to content

Commit 22768d8

Browse files
committed
新增url解析工具类
1 parent 2fe9473 commit 22768d8

2 files changed

Lines changed: 43 additions & 0 deletions

File tree

test/test_urlparse.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# encoding: utf-8
2+
__author__ = 'zhanghe'
3+
4+
import sys
5+
sys.path.append('..')
6+
from tools.url import get_next_url
7+
8+
print get_next_url('http://www.163.com/mail/index.htm', 'http://www.163.com/about.htm')
9+
print get_next_url('http://www.163.com/mail/index.htm', '/about.htm')
10+
print get_next_url('http://www.163.com/mail/index.htm', 'about.htm')

tools/url.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# encoding: utf-8
2+
__author__ = 'zhanghe'
3+
4+
from urlparse import urlparse
5+
from urlparse import urlunparse
6+
from urlparse import urljoin
7+
8+
9+
def get_next_url(current_url, path):
10+
"""
11+
组装url
12+
:param current_url:
13+
:param path:
14+
:return:
15+
"""
16+
if path is None or path == '':
17+
return ''
18+
if path.startswith('http'):
19+
return path
20+
if path.startswith('/'):
21+
url = urlparse(current_url)
22+
return urlunparse((url.scheme, url.netloc, path, url.params, url.query, url.fragment))
23+
return urljoin(current_url, path)
24+
25+
26+
def test():
27+
print get_next_url('http://www.163.com/mail/index.htm', 'http://www.163.com/about.htm')
28+
print get_next_url('http://www.163.com/mail/index.htm', '/about.htm')
29+
print get_next_url('http://www.163.com/mail/index.htm', 'about.htm')
30+
31+
32+
if __name__ == '__main__':
33+
test()

0 commit comments

Comments
 (0)