get:name = urllib.parse.quote('周杰伦')
post:data = urllib.parse.urlencode(data).encode('utf-8')
为什么get请求不用utf-8进行编码,而post请求需要用utf-8进行编码?
希望有人可以帮我解答,非常感谢!
get:name = urllib.parse.quote('周杰伦')
post:data = urllib.parse.urlencode(data).encode('utf-8')
为什么get请求不用utf-8进行编码,而post请求需要用utf-8进行编码?
希望有人可以帮我解答,非常感谢!
其实是都需要的,只不过因为基本库urllib对于post请求需要显式的调用,把需要传输的数据编码为byte类型,单独传输,而get请求只不过是构造url,在urlopen那步就都处理了
POST
import urllib.parse
import urllib.request
url = 'http://www.someserver.com/cgi-bin/register.cgi'
values = {'name' : 'Michael Foord',
'location' : 'Northampton',
'language' : 'Python' }
data = urllib.parse.urlencode(values)
data = data.encode('ascii') # data should be bytes
req = urllib.request.Request(url, data)
with urllib.request.urlopen(req) as response:
the_page = response.read()
GET
>>> import urllib.request
>>> import urllib.parse
>>> data = {}
>>> data['name'] = 'Somebody Here'
>>> data['location'] = 'Northampton'
>>> data['language'] = 'Python'
>>> url_values = urllib.parse.urlencode(data)
>>> print(url_values) # The order may differ from below.
name=Somebody+Here&language=Python&location=Northampton
>>> url = 'http://www.example.com/example.cgi'
>>> full_url = url + '?' + url_values
>>> data = urllib.request.urlopen(full_url)