用于 dict python 的 URL 查询参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/21584545/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
URL query parameters to dict python
提问by Leonardo Andrade
Is there a way to parse a URL (with some python library) and return a python dictionary with the keys and values of a query parameters part of the URL?
有没有办法解析一个 URL(使用一些 python 库)并返回一个 python 字典,其中包含 URL 的查询参数部分的键和值?
For example:
例如:
url = "http://www.example.org/default.html?ct=32&op=92&item=98"
expected return:
预期收益:
{'ct':32, 'op':92, 'item':98}
采纳答案by Martijn Pieters
Use the urllib.parselibrary:
>>> from urllib import parse
>>> url = "http://www.example.org/default.html?ct=32&op=92&item=98"
>>> parse.urlsplit(url)
SplitResult(scheme='http', netloc='www.example.org', path='/default.html', query='ct=32&op=92&item=98', fragment='')
>>> parse.parse_qs(parse.urlsplit(url).query)
{'item': ['98'], 'op': ['92'], 'ct': ['32']}
>>> dict(parse.parse_qsl(parse.urlsplit(url).query))
{'item': '98', 'op': '92', 'ct': '32'}
The urllib.parse.parse_qs()and urllib.parse.parse_qsl()methods parse out query strings, taking into account that keys can occur more than once and that order may matter.
该urllib.parse.parse_qs()和urllib.parse.parse_qsl()方法解析出查询字符串,考虑到钥匙可能会出现不止一次和顺序可能无关紧要。
If you are still on Python 2, urllib.parsewas called urlparse.
如果您仍在使用 Python 2,urllib.parse则调用urlparse.
回答by reubano
For Python 3, the values of the dict from parse_qsare in a list, because there might be multiple values. If you just want the first one:
对于 Python 3,dict from 的值parse_qs在一个列表中,因为可能有多个值。如果你只想要第一个:
>>> from urllib.parse import urlsplit, parse_qs
>>>
>>> url = "http://www.example.org/default.html?ct=32&op=92&item=98"
>>> query = urlsplit(url).query
>>> params = parse_qs(query)
>>> params
{'item': ['98'], 'op': ['92'], 'ct': ['32']}
>>> dict(params)
{'item': ['98'], 'op': ['92'], 'ct': ['32']}
>>> {k: v[0] for k, v in params.items()}
{'item': '98', 'op': '92', 'ct': '32'}
回答by Tomos Williams
If you prefer not to use a parser:
如果您不想使用解析器:
url = "http://www.example.org/default.html?ct=32&op=92&item=98"
url = url.split("?")[1]
dict = {x[0] : x[1] for x in [x.split("=") for x in url[1:].split("&") ]}
So I won't delete what's above but it's definitely not what you should use.
所以我不会删除上面的内容,但它绝对不是你应该使用的。
I think I read a few of the answers and they looked a little complicated, incase you're like me, don't use my solution.
我想我阅读了一些答案,它们看起来有点复杂,如果您像我一样,请不要使用我的解决方案。
Use this:
用这个:
from urllib import parse
params = dict(parse.parse_qsl(parse.urlsplit(url).query))
and for Python 2.X
和 Python 2.X
import urlparse as parse
params = dict(parse.parse_qsl(parse.urlsplit(url).query))
I know this is the same as the accepted answer, just in a one liner that can be copied.
我知道这与接受的答案相同,只是在一个可以复制的衬垫中。
回答by Anurag Misra
For python 2.7
对于python 2.7
In [14]: url = "http://www.example.org/default.html?ct=32&op=92&item=98"
In [15]: from urlparse import urlparse, parse_qsl
In [16]: parse_url = urlparse(url)
In [17]: query_dict = dict(parse_qsl(parse_url.query))
In [18]: query_dict
Out[18]: {'ct': '32', 'item': '98', 'op': '92'}
回答by Clarius
I agree about not reinventing the wheel but sometimes (while you're learning) it helps to build a wheel in order to understand a wheel. :) So, from a purely academic perspective, I offer this with the caveat that using a dictionary assumes that name value pairs are unique (that the query string does not contain multiple records).
我同意不重新发明轮子,但有时(在您学习时)它有助于构建一个轮子以了解轮子。:) 所以,从纯粹的学术角度来看,我提出这个警告,即使用字典假设名称值对是唯一的(查询字符串不包含多个记录)。
url = 'http:/mypage.html?one=1&two=2&three=3'
page, query = url.split('?')
names_values_dict = dict(pair.split('=') for pair in query.split('&'))
names_values_list = [pair.split('=') for pair in query.split('&')]
I'm using version 3.6.5 in the Idle IDE.
我在空闲 IDE 中使用 3.6.5 版。
回答by Tamim
For python2.7I am using urlparsemodule to parse url query to dict. 
因为python2.7我正在使用urlparse模块将 url 查询解析为 dict。
import urlparse
url = "http://www.example.org/default.html?ct=32&op=92&item=98"
print urlparse.parse_qs( urlparse.urlparse(url).query )
# result: {'item': ['98'], 'op': ['92'], 'ct': ['32']} 

