This is normal when the URL address contains Chinese characters or the parameter has Chinese characters, but when such a URL is passed as a parameter (the most common callback ), encode and convert some Chinese characters or even.
1. urlencode
There is a urlencode function in the urllib library, which can convert key-value pairs such as key-value into the desired format and return strings such as a = 1 & B = 2, for example:
>>>
From
Urllib
Import
Urlencode
>>> Data
=
{
...
'A'
:
'Test'
,
...
'Name'
:
'Warcraft'
...}
>>>
Print
Urlencode (data)
A
=
Test & name
=
%
C4
%
A7
%
CA
%
De
What if you only want to perform urlencode conversion on a string? Urllib provides another function: quote ()
>>>
From
Urllib
Import
Quote
>>> Quote (
'Warcraft'
)
'% C4 % A7 % Ca % de'
Ii. urldecode
When the string after urlencode is passed, the URL decode will be decoded after acceptance. Urllib provides the unquote () function, and there is no urldecode ()!
>>>
From
Urllib
Import
Unquote
>>> Unquote (
'% C4 % A7 % Ca % de'
)
'\ Xc4 \ Xa7 \ xca \ xde'
>>>
Print
Unquote (
'% C4 % A7 % Ca % de'
)
Warcraft
Iii. Discussion
When urldecode is used, the output of the unquote () function corresponds to the encoding of Chinese Characters in GBK. It is not difficult to find out the result of comparing quote, the so-called urlencode is to convert the string into GBK encoding, and then replace \ x with %. If your terminal is utf8 encoded, convert the result to utf8 output. Otherwise, garbled characters will occur.
You can customize or rewrite functions such as urlencode () and urldecode () according to the actual situation.