我需要一些帮助,我正在努力做到:
修改每个键的单个字典值(在这种情况下为param
),一次只为一个参数,每次迭代的值为self.fooz
。
喜欢这个
这样的网址就像:
somesite.com?id=6&name=Bill
一样somesite.com?id=<self.fooz>&name=Bill
(迭代数量 个人愚蠢)然后,somesite.com?id=6&name=<self.fooz>
(迭代个别傻瓜的数量)
最后,生成full_param_vector
和full_param
值,如下所述
有人可以帮帮我吗?
我做过:
1)通过self.path_object
引入一组原始路径
2)在?
之后解析路径以获取所有原始参数化key/values
(通过parse_after
)
我写下了一些我想要完成的伪代码:
if self.path_object is not None:
dictpath = {}
for path in self.path_object:
#path.pathToScan - returns a full url e.g. somesite.com?id=6&name=Bill
#parse_after returns a string with parameters only, like: {u'id': [u'2'], u'name': [u'Dog']}
parse_after = urlparse.parse_qs(path.pathToScan[path.pathToScan.find('?') + 1:], keep_blank_values=0, strict_parsing=0)
#for each params in 'parse_after':
#replace a key's value from params with a value from self.foozs,
#loop over this single key inserting a single value from self.fooz for each param for all fooz_objects, then continue to the next param and do the same
#create full_param_vector var with these new values
#construct full_path made up of: path.pathToScan - <part before '?'> + "?" + full_param_vector
#add all 'full_path' to a dictionary named dictpath
#print dictpath
欢迎任何帮助。谢谢
答案 0 :(得分:0)
这样的事情可以解决问题,但是,我仍然无法解析你的问题
from collections import defaultdict
import urllib
import urlparse
# parse the url into parts
parsed = urlparse.urlparse('http://somesite.com/blog/posting/?id=6&name=Bill')
# and parse the query string into a dictionary
qs = urlparse.parse_qs(parsed.query, keep_blank_values=0, strict_parsing=0)
# this makes a new dictionary, with same keys, but all values changed to "foobar"
foozsified = { i: 'foobar' for i in qs }
# make them back to a query string: id=foobar&name=foobar
quoted = urllib.urlencode(foozsified, doseq=True)
# the original parsed result is a named tuple and cannot be changed,
# make it into a list
parsed = list(parsed)
# replace the 4th element - the query string with our new
parsed[4] = quoted
# and unparse it into a full url
print(urlparse.urlunparse(parsed))
打印
http://somesite.com/blog/posting/?id=foobar&name=foobar
因此您可以对qs
词典进行任何修改,然后使用urlunparse
返回完整的网址。