Python 拆分a&;上的查询字符串;char获取不同的参数;蟒蛇:

Python 拆分a&;上的查询字符串;char获取不同的参数;蟒蛇:,python,file,url,split,char,Python,File,Url,Split,Char,我希望在“&”字符上拆分当前查询字符串,以便获得不同的查询参数。根据这些参数,我希望将它们放入不同的文件中,即p_file.txt、blog_file.txt、portfolio_file.txt等。我一直在尝试拆分查询列表,但这是不可能的。我愿意寻求帮助 def parse_file(): # Open the file for reading infile = open("URLlist.txt", 'r') # Read every single line of t

我希望在“&”字符上拆分当前查询字符串,以便获得不同的查询参数。根据这些参数,我希望将它们放入不同的文件中,即p_file.txt、blog_file.txt、portfolio_file.txt等。我一直在尝试拆分查询列表,但这是不可能的。我愿意寻求帮助

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()

我希望你会喜欢。

queryvars=url.query.split('&')有效吗?是的!它在“&”字符上拆分。我怎样才能把被分割成不同文件的东西放进去?例如:p=49->p\u file.txt,附件id=32->附件id\u文件。txt@kindall实际上,他建议使用parse_qs可能更好。这会给你一本字典,例如,
{'p':['49'],'attachment_id':['32']}
。然后,您可以使用例如
for key-in-queryvars:filename=key+'.\u-file.txt'[…]
对其进行迭代。因此:
url=parse\u-qsl(urlparse(line)[4])
for key-in-url:p\u-file=key+'p\u-file.txt'如果存在“p=”,则写入该文件