Python urlencode encoding [Python Club]
Python Urlencode Encoding Urlencode Call Method
UrlencodeMust be a dictionary
D ={'Par1':'A','Par2':'B',}Print Urllib.Urlencode(M)# Par2 = B & par1 =
Urlencode Encoding
FunctionUrlencodeThe original encoding of the input parameter is not changed, that is, the encoding of the post or get parameter must be adjusted before the call.PythonEncoding conversion can refer to http://www.pythonclub.org/Python-Basic/codec.
Problem: the request is now simulated by Google and Baidu. Because Baidu uses gb2312 encoding, Google uses utf8 encoding.UrlencodeThe values are different. The following uses "Empire" as an example:
PythonFile
# Coding: UTF-8
Run urllib.Urlencode("Empire"): % E5 % B8 % 9d % E5 % 9B % BD.UrlencodeThe result is the utf8 encoded "Empire ".
What should I do if I want to get the "Empire" of gb2312 code?
St = u'Imperial'St = ST.Encode('Gb2312')M ={'Par': ST,}S =Urllib.Urlencode(M)PrintS# The result is par = % B5 % db % B9 % fa.
DjangoUrlencodeSimilarly, the method is as follows:
FromDjango.Utils.HTTP ImportUrlquotea = urlquote('Imperial')PrintA
Obtain the GBK encoding of Chinese Characters
In fact, you can use the quote function of urllib to convert Chinese in the URL into GBK encoding. The resulting encoding is a URL that complies with the URI standard.
>>> Import urllib >>> A = "Empire" >>> a' \ xb5 \ XDB \ xb9 \ xfa '>>> urllib. quote (a) '% B5 % db % B9 % fa' >>>