1

What I'm trying to do: Output the value of a self.out var in a way that I can still use in another method.

The code

if self.path_object is not None:
    dictpath = {}
    for path in self.path_object:
        parsed = urlparse(path.pathToScan)
        if parsed.query:
            self.params = parsed.query.split('&', 2)
            self.out = list(map(lambda v: v.split("=")[0] +"=" + str(self.fooz), self.params))
            dictpath[parsed.geturl()] = self.out
    print dictpath

This code brings in a set of paths (via path_object), it then parses those paths on the & symbol and attaches the value of self.fooz to it, then puts it into a dictionary and outputs the results.

Result Its currently outputting a list like (added spacing for readability):

{
u'www.somesite.com/param.php?id=317':
    u'id=[<self.fooz>, <self.fooz>, <self.fooz>,<self.fooz>, <self.fooz>, <self.fooz>]', 

u'somesite.com/param.php?id=911&param2=6&param3=cat': 
    [u'id=[<self.fooz>, <self.fooz>, <self.fooz>,<self.fooz>, <self.fooz>, <self.fooz>]', 
    u'param2=[<self.fooz>, <self.fooz>, <self.fooz>,<self.fooz>, <self.fooz>, <self.fooz>]', 
    u'param3=[<self.fooz>, <self.fooz>, <self.fooz>,<self.fooz>, <self.fooz>, <self.fooz>]']
}

My question is, how can I access this output in another method, so that I can loop through each parameter like:

u'somesite.com/param.php?id=<<<<<self.fooz>>>>>&param2=6&param3=cat': 

then

u'somesite.com/param.php?id=911&param2=<<<<<self.fooz>>>>>&param3=cat

and then so on for each parameter in a path.

So, far I have tried requesting a url from the present output, but requests isn't allowing a path to be requested like <self.fooz> as its not a valid url (e.g. including somesite.com or http://somesite.com).

Any help is greatly appreciated.

Thank you SO much!

1 Answer 1

1

You have to parse the string again. Use urlparse.parse_qs (https://docs.python.org/2/library/urlparse.html). Do it like this:

import urlparse
s = u'somesite.com/param.php?id=911&param2=<<<<<self.fooz>>>>>&param3=cat'
params = urlparse.parse_qs(s[s.find('?') + 1:])
print params

The result is:

{u'param3': [u'cat'], u'id': [u'911'], u'param2': [u'<<<<<self.fooz>>>>>']}

Now you can iterate over params.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the suggestion. Could you show an example in this case of how to go about this?
I see how this is working now. I really appreciate it @pts ! Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.