简体   繁体   中英

How to deal with `;` with `urllib.parse.parse_qsl()`?

; can not be dealt by parse_qsl() . Is there a way to make it aware of ; ? Thanks.

>>> import urllib.parse
>>> urllib.parse.parse_qsl('http://example.com/?q=abc&p=1;2;3')
[('http://example.com/?q', 'abc'), ('p', '1')]

It would be best to make sure that the URLs you are dealing with have the semicolons URL encoded. eg http://example.com/?q=abc&p=1%3B2%3B3

If for some reason you can't do the above, you could do something like this:

from urllib.parse import urlparse, unquote_plus

url = "http://example.com/?q=abc&p=1;2;3"
parts = urlparse(url)
qs = parts.query
pairs = [p.split("=", 1) for p in qs.split("&")]
decoded = [(unquote_plus(k), unquote_plus(v)) for (k, v) in pairs]
>>> decoded
[('q', 'abc'), ('p', '1;2;3')]

The above code assumes a few things about the query string. eg that all keys have values. If you want something that makes fewer assumptions, see the parse_qsl source code .

Actually, it does treat them correctly (as delimiters). You just have to tell it to keep blank values:

>>> urllib.parse.parse_qsl('q=abc&p=1;2;3', keep_blank_values=True)
[('q', 'abc'), ('p', '1'), ('2', ''), ('3', '')]

Note that you should not be passing the entire url to parse_qsl , only the querystring part.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM