0

I'm looking to split the current query string on a '&' char so I can get different query arguments. From those arguments I am looking to put them into different files, i.e. p_file.txt, blog_file.txt, portfolio_file.txt, etc. I have been stuck trying to split a list of queries but it is not possible. I am open for help.

def parse_file():
    # Open the file for reading
    infile = open("URLlist.txt", 'r')
    # Read every single line of the file into an array of lines
    lines = infile.readlines()

    # For every line in the array of lines, do something with that line
    for line in lines:
        # The lines we get back from readlines will have a newline
        # character appended.  So, let's strip that out as we parse
        # the URL from the line into its components
        line = line.strip()
        url = urlparse(line)
        # If the url has a query component...(ie. url.query)
        if url.query:
            # ...then print it out!  We need to strip the trailing newline
            # character from the url query, because urlparse doesn't do that
            # for us.  
            queryvars = url.query
            print queryvars
            #for q in queryvars:
                 #print q
       parse_file()
5
  • 1
    Does queryvars = url.query.split('&') work? Commented Oct 16, 2013 at 18:23
  • Yes! It is splitting upon the '&' char. How would I be able to put whatever is split into different files? ex: p=49 -> p_file.txt, attachment_id = 32 -> attachment_id_file.txt Commented Oct 16, 2013 at 18:35
  • @kindall's suggestion to use parse_qs is probably better, actually. That will give you a dictionary, e.g. { 'p': ['49'], 'attachment_id': ['32'] }. You can then iterate over that with e.g. for key in queryvars: filename = key + '_file.txt' [...]. Commented Oct 16, 2013 at 18:44
  • so: url = parse_qsl(urlparse(line)[4]) for key in url: p_file = key + 'p_file.txt' Commented Oct 16, 2013 at 19:08
  • if there is a 'p=', write out to the file Commented Oct 16, 2013 at 19:09

1 Answer 1

1

I expect you want urlparse.parse_qs.

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

Comments

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.