Parse query part from url

删除回忆录丶 提交于 2019-12-17 18:49:28

问题


I want to parse query part from url, this is my code to do this:

>>> from urlparse import urlparse, parse_qs
>>> url = '/?param1&param2=2'
>>> parse_qs(urlparse(url).query)
>>> {'param2': ['23']}

This code looks good, but "parse_qs" method loses query parameters like "param1" or "param1=". Can I parse query part with stantard library and save all parameters?


回答1:


You want:

from urlparse import parse_qs, urlparse

parse_qs(urlparse(url).query, keep_blank_values=True)
# {'param2': ['2'], 'param1': ['']}


来源:https://stackoverflow.com/questions/11600681/parse-query-part-from-url

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!