url中参数值的替换

今天有个群里的朋友问了个问题:就是如何在url里替换参数值?

我就研究了一下:可能有更好的方法,我们一起分享:

解决方案:

 1 # -*- coding: utf-8 -*-
 2 
 3 import urlparse, copy, urllib
 4 
 5 def url_R_value(url, val):
 6 
 7     list1 = []
 8 
 9     u = urlparse.urlparse(url)
10     
11     qs = u.query
12     
13     pre_url = url.replace('?'+qs, '')
14 
15     qs_dict = dict(urlparse.parse_qsl(qs))
16 
17     for i in qs_dict.keys():
18 
19         tmp_dict = copy.deepcopy(qs_dict)
20 
21         tmp_dict[i] = val
22 
23         tmp_qs = urllib.unquote(urllib.urlencode(tmp_dict))
24 
25         list1.append(pre_url + "?" + tmp_qs)
26 
27     return list1
28 
29 url = "/2013-12-26/Accounts/4aea47a5588bd37e015894e48bed000e/AccountInfo?sig=08979F3899A31CD517595B5D081C3BEA"
30 
31 value_str = '88888'
32 
33 urls = url_R_value(url, value_str)
34 
35 for p_url in urls:
36     print p_url

 

posted @ 2017-03-07 16:22  JodieWang  阅读(4167)  评论(0编辑  收藏  举报