Skip to content

Commit 35d1e8d

Browse files
committed
更新url操作类
1 parent 463395b commit 35d1e8d

1 file changed

Lines changed: 21 additions & 5 deletions

File tree

tools/url.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# encoding: utf-8
22
__author__ = 'zhanghe'
33

4-
from urlparse import urlparse
4+
from urlparse import urlparse, parse_qs
55
from urlparse import urlunparse
66
from urlparse import urljoin
77

@@ -25,6 +25,20 @@ def get_next_url(current_url, next_url):
2525
return urljoin(current_url, next_url)
2626

2727

28+
def get_url_param_value(url, param_key):
29+
"""
30+
获取链接查询参数值
31+
:param url:
32+
:param param_key:
33+
:return:
34+
"""
35+
result = urlparse(url)
36+
params = parse_qs(result.query, True)
37+
param_value = params.get(param_key)
38+
print result, '\n', params, '\n', param_value
39+
return ','.join(param_value)
40+
41+
2842
def test():
2943
print get_next_url('http://www.163.com/mail/index.htm', 'http://www.163.com/about.htm')
3044
print urljoin('http://www.163.com/mail/index.htm', 'http://www.163.com/about.htm')
@@ -40,10 +54,12 @@ def test():
4054

4155

4256
if __name__ == '__main__':
43-
test()
44-
test_url = 'http://suining.58.com/zhongdiangong/?sort=pingfen'
45-
print urlparse(test_url).hostname.rstrip('.58.com')
46-
print urlparse(test_url).path.strip('/')
57+
# test()
58+
# test_url = 'http://suining.58.com/zhongdiangong/?sort=pingfen'
59+
# print urlparse(test_url).hostname.rstrip('.58.com')
60+
# print urlparse(test_url).path.strip('/')
61+
test_url = 'http://localhost/test.py?a=hello&b=world'
62+
print get_url_param_value(test_url, 'a')
4763

4864

4965
"""

0 commit comments

Comments
 (0)