Parsing URI parameter and keyword value pairs

后端 未结 3 1574
情话喂你
情话喂你 2021-01-22 12:45

I would like to parse the parameter and keyword values from URI/L\'s in a text file. Parameters without values should also be included. Python is fine but am open to suggestion

3条回答
  •  无人及你
    2021-01-22 13:42

    You don't need to dive into fragile regex world.

    urlparse.parse_qsl() is the tool for the job (urllib.quote() helps to escape special characters):

    from urllib import quote
    from urlparse import parse_qsl, urlparse
    
    
    with open('links.txt') as f:
        for url in f:
            params = parse_qsl(urlparse(url.strip()).query, keep_blank_values=True)
            for key, value in params:
                print "%s=%s" % (key, quote(value))
    

    Prints:

    date=2012-11-20
    l=user
    x=0
    id=1
    page=http%3A//domain.com/page.html
    unique=123456
    refer=http%3A//domain2.net/results.aspx%3Fq%3Dbob%20test%201.21%20some%26file%3Dname
    text=
    l=adm
    y=5
    id=2
    page=http%3A//support.domain.com/downloads/index.asp
    unique=12345
    view=month
    date=2011-12-10
    

    Hope that helps.

提交回复
热议问题