我希望将 URL 参数作为“真实”字典。我怎样才能做到这一点?我不想params.split()
多次使用。
from urllib.parse import urlparse
url = 'http://nohost.com?params=depth:1,width:500,size:small'
the_url = urlparse(url)
url_part, params = the_url.path, the_url.query
print(params) # params=depth:1,width:500,size:small
归根结底,我想从 URL 参数中得到一个真正的字典
params = {'depth': 1, 'width': 500, 'size': 'small'}
您必须自己解析参数。正如 @jonrsharpe 在评论中指出的那样,您可以选择在解析 urlparse 的输出后使用 parse_qs 获取列表的字典,然后几乎实现解析逻辑。
像这样的事情,