0

How can I split urls like this (which are coming from a django object selection):

[<PathsOfDomain: www.somesite.com/>, <PathsOfDomain: somesite.com/prof.php?pID=589>, <PathsOfDomain: www.somesite.com/some/path/here/paramid=6, <PathsOfDomain: www.somesite.com/prof.php?pID=317>, <PathsOfDomain: www.somesite.com/prof.php?pID=523>]

I have code:

if self.path_object is not None:
    dictpath = {}
    for path in self.path_object:
        print path #debugging only
        self.params = path.pathToScan.split("?")[1].split("&")
        out = list(map(lambda v: v.split("=")[0] +"=" + self.fuzz_vectors, self.params))
        dictpath[path] = out
    print dictpath

I'm getting an error of:

self.params = path.pathToScan.split("?")[1].split("&")
IndexError: list index out of range

What am I doing wrong here?

Thank you!

4
  • 1
    It looks like a django queryset. Show PathsOfDomain model definition. Thanks. Commented May 27, 2014 at 20:52
  • can you give an actual example of what you want the dictionary to be after you are done? Commented May 27, 2014 at 21:17
  • Added some updated code, with an error message Im getting Commented May 27, 2014 at 21:21
  • If there's no querystring in the URL, then split("?")[1] will be out of range, as you can see, e.g. "www.somesite.com".split("?") equals ["www.somesite.com"]. Commented May 28, 2014 at 1:53

1 Answer 1

2
self.params = path.split("?")[1].split("&")

should be

self.params = path.path.split("?")[1].split("&")

path is the PathsOfDomain object, but you need path.path which the actual string containing the path.

You should also look at the urlparse module which contains code to help parsing urls. You can use it simplify your code here.

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

10 Comments

Thanks for the code snippets, however, I'm still receiving an error of: IndexError: list index out of range
Any thoughts on this?
I can see the problem, but I'm going to have to see some initiative in figuring it out before telling you what it is.
I did some background on this error, it appears as I loop over the paths, its decrementing the list by one and thus throwing off the loop? Since its trying to access indices which no longer exist? Is this what is happening here Winston?
No, that particular issue isn't happening here. That happens when you somehow modify the list you are operating on. But you aren't modifying the list so you are safe from that. Can you determine which path it fails on?
|

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.