When the URL address contains Chinese or "/", this is required for UrlEncode encoding conversion.
First, UrlEncode
The UrlEncode parameter is a dictionary that converts key-value pairs such as key-value into the format we want. If you're using Python2.*,urlencode in Urllib.urlencode. If you're using Python3,urlencode in Urllib.parse.urlencode,
For example
Import urllib.parsedata={"name": "Wang Nima", "Age": "/", "addr": "ABCdef"}print (Urllib.parse.urlencode (data))
Output to
addr=abcdef&name=%e7%8e%8b%e5%b0%bc%e7%8e%9b&age=%2f
What if you only want to convert a string to UrlEncode? Urllib provides another function: quote ()
Print (Urllib.parse.quote ("Hahaha Hello! "))
Output to
hahaha%e4%bd%a0%e5%a5%bd%e5%95%8a%ef%bc%81
Second, unquotewhen the string after the UrlEncode is passed over, the acceptance will decode the--urldecode. Urllib provides unquote () This function, can not urldecode ()!
Import urllib.parsedata={"name": "Wang Nima", "Age": "/", "addr": "ABCdef"}print (Urllib.parse.urlencode (data)) print ( Urllib.parse.quote ("Hahaha Hello!" ")) Print (Urllib.parse.unquote (" hahaha%e4%bd%a0%e5%a5%bd%e5%95%8a%ef%bc%81 "))
Output
addr=abcdef&name=%e7%8e%8b%e5%b0%bc%e7%8e%9b&age=%2fhahaha%e4%bd%a0%e5%a5%bd%e5%95%8a%ef%bc% 81hahaha Hello!
When doing urldecode, see unquote () This function output, is corresponding to the Chinese code in the GBK, in contrast to the results of quote () is not difficult to find, the so-called UrlEncode is the string interchange GBK code, and then replace the \x to%. If your terminal is UTF8 encoded, then to turn the results into UTF8 output, otherwise garbled.
Functions such as UrlEncode (), UrlDecode () can be customized or rewritten according to the actual situation.
Python rookie Promotion one----UrlEncode and unquote