[英]Best way to parse a URL query string
What is the best way to parse data out of a URL query string (for instance, data appended to the URL by a form) in python? 在python中解析URL查询字符串(例如,表单附加到URL的数据)中的数据的最佳方法是什么? My goal is to accept form data and display it on the same page.
我的目标是接受表单数据并将其显示在同一页面上。 I've researched several methods that aren't quite what I'm looking for.
我研究了几种不太适合我的方法。
I'm creating a simple web server with the goal of learning about sockets. 我正在创建一个简单的Web服务器,目的是学习套接字。 This web server won't be used for anything but testing purposes.
此Web服务器不会用于任何其他测试目的。
GET /?1pm=sample&2pm=&3pm=&4pm=&5pm= HTTP/1.1
Host: localhost:50000
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:11.0) Gecko/20100101 Firefox/11.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive
Referer: http://localhost:50000/?1pm=sample&2pm=&3pm=&4pm=&5pm=
The urllib.parse module is your friend: https://docs.python.org/3/library/urllib.parse.html urllib.parse模块是你的朋友: https ://docs.python.org/3/library/urllib.parse.html
Check out urllib.parse.parse_qs (parsing a query-string, ie form data sent to server by GET or form data posted by POST, at least for non-multipart data). 查看urllib.parse.parse_qs (解析查询字符串,即通过GET发送到服务器的表单数据或POST发布的表单数据,至少对于非多部分数据)。 There's also cgi.FieldStorage for interpreting multipart-data.
还有cgi.FieldStorage用于解释多部分数据。
For parsing the rest of an HTTP interaction, see RFC2616 , which is the HTTP/1.1 protocol specification. 有关解析HTTP交互的其余部分,请参阅RFC2616 ,它是HTTP / 1.1协议规范。
Here is an example using python3 urllib.parse
: 以下是使用python3
urllib.parse
的示例:
from urllib.parse import urlparse, parse_qs
URL='https://someurl.com/with/query_string?i=main&mode=front&sid=12ab&enc=+Hello'
parsed_url = urlparse(URL)
parse_qs(parsed_url.query)
output: 输出:
{'i': ['main'], 'enc': [' Hello '], 'mode': ['front'], 'sid': ['12ab']}
Note for python2: from urlparse import urlparse, parse_qs
注意python2:
from urlparse import urlparse, parse_qs
SEE: https://pythonhosted.org/six/#module-six.moves.urllib.parse 请参阅: https : //pythonhosted.org/six/#module-six.moves.urllib.parse
If you need unique key from query string, use dict()
with parse_qsl()
如果您需要来自查询字符串的唯一键,
parse_qsl()
dict()
与parse_qsl()
import urllib.parse
urllib.parse.urlparse('https://someurl.com/with/query_string?a=1&b=2&b=3').query
a=1&b=2&b=3
urllib.parse.parse_qs('a=1&b=2&b=3');
{'a': ['1'], 'b': ['2','3']}
urllib.parse.parse_qsl('a=1&b=2&b=3')
[('a', '1'), ('b', '2'), ('b', '3')]
dict(urllib.parse.parse_qsl('a=1&b=2&b=3'))
{'a': '1', 'b': '3'}
built into python 2.7 内置于python 2.7中
>>> from urlparse import parse_qs
>>> parse_qs("search=quint&tags=python")
{'search': ['quint'], 'tags': ['python']}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.