python - Use urlparse library to iterate through url param -
i need following i'm trying do: modify individual dictionary values each key (a param
in case) single param @ time value self.fooz
each iteration.
like this
so url like:
somesite.com?id=6&name=bill
becomesomesite.com?id=<self.fooz>&name=bill
(iterating number of individual foozes) then,somesite.com?id=6&name=<self.fooz>
(iterating number of individual foozes)
finally, generating full_param_vector
, full_param
values discussed below
can please me?
i've done: 1) set of raw paths brought in via self.path_object
) 2) parse paths after ?
grab raw parametrized key/values
(via parse_after
)
i wrote down pseudocode of i'm trying accomplish:
if self.path_object not none: dictpath = {} path in self.path_object: #path.pathtoscan - returns full url e.g. somesite.com?id=6&name=bill #parse_after returns string 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 key's value params value self.foozs, #loop on single key inserting single value self.fooz each param fooz_objects, continue next param , same #create full_param_vector var these new values #construct full_path made of: path.pathtoscan - <part before '?'> + "?" + full_param_vector #add 'full_path' dictionary named dictpath #print dictpath
any welcome. thank you
something trick, however, still not quite parse your question
from collections import defaultdict import urllib import urlparse # parse url parts parsed = urlparse.urlparse('http://somesite.com/blog/posting/?id=6&name=bill') # , parse query string dictionary qs = urlparse.parse_qs(parsed.query, keep_blank_values=0, strict_parsing=0) # makes new dictionary, same keys, values changed "foobar" foozsified = { i: 'foobar' in qs } # make them query string: id=foobar&name=foobar quoted = urllib.urlencode(foozsified, doseq=true) # original parsed result named tuple , cannot changed, # make list parsed = list(parsed) # replace 4th element - query string our new parsed[4] = quoted # , unparse full url print(urlparse.urlunparse(parsed))
prints
http://somesite.com/blog/posting/?id=foobar&name=foobar
thus can modifications qs
dictionary there, , use urlunparse
full url.
Comments
Post a Comment