11# encoding: utf-8
22__author__ = 'zhanghe'
33
4- from urlparse import urlparse
4+ from urlparse import urlparse , parse_qs
55from urlparse import urlunparse
66from 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+
2842def 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
4256if __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