tl;dr
This section summarizes how to parse URLs and query parameters in Python and convert percent encoding.
In Python3, modules such as URL acquisition and parsing are unified to ʻurllib`. The correspondence table is summarized below.
Python2 | Python3 | |
---|---|---|
URL parse | urlparse.urlparse | urllib.parse.urlparse |
Query string parsing | urlparse.parse_qs | urllib.parse.parse_qs |
Query parameter creation | urllib.urlencode | urllib.parse.urlencode |
escape | urllib.quote | urllib.parse.quote |
Escape restore | urllib.unquote | urllib.parse.unquote |
Python3
The code below assumes an import of ʻurllib`.
import urllib
urllib.parse.urlparse("http://example.com/example.html?foo=bar&hoge=fuga")
# => ParseResult(scheme='http', netloc='example.com', path='/example.html', params='', query='foo=bar&hoge=fuga', fragment='')
urllib.parse.parse_qs("foo=bar&hoge=fuga")
# => {'hoge': ['fuga'], 'foo': ['bar']}
urllib.parse.parse_qs("query=%E3%83%86%E3%82%B9%E3%83%88")
# => {'query': ['test']}
urllib.parse.urlencode({"query":"test"})
# => query=%E3%83%86%E3%82%B9%E3%83%88
urllib.parse.quote("test")
# => %E3%83%86%E3%82%B9%E3%83%88
urllib.parse.unquote("%E3%83%86%E3%82%B9%E3%83%88")
# =>test
Python2
The code below assumes the import of ʻurllib and ʻurlparse
.
import urllib
import urlparse
urlparse.urlparse("http://example.com/example.html?foo=bar&hoge=fuga")
# => ParseResult(scheme='http', netloc='example.com', path='/example.html', params='', query='foo=bar&hoge=fuga', fragment='')
urlparse.parse_qs("foo=bar&hoge=fuga")
# => {'hoge': ['fuga'], 'foo': ['bar']}
urlparse.parse_qs("query=%E3%83%86%E3%82%B9%E3%83%88")
# => {'query': ['test']}
http://docs.python.jp/2/library/urlparse.html#urlparse.parse_qs
urllib.urlencode({"query":"test"})
# => query=%E3%83%86%E3%82%B9%E3%83%88
urllib.quote("test")
# => %E3%83%86%E3%82%B9%E3%83%88
urllib.unquote("%E3%83%86%E3%82%B9%E3%83%88")
# =>test
Recommended Posts