UrlEncode Calling Method
The UrlEncode parameter must be dictionary
Import urllibd = {' name1 ': ' www.pythontab.com ', ' name2 ': ' bbs.pythontab.com '}print urllib.urlencode (d)
Output:
Name2=bbs.pythontab.com&name1=www.pythontab.com
The equivalent of stitching two URL parameters, this usage is similar to PHP http_build_query (), here is not the majority of PHP in how to use, interested in their own to check.
UrlEncode encoding
The function UrlEncode does not alter the original encoding of the incoming parameter, which means that the encoding of the post or get parameter needs to be adjusted before the call.
Problem: Now the simulation request Google and Baidu, because Baidu is using gb2312 encoding, Google is using UTF8 encoding, two sites submitted to the URL of the Chinese parameter UrlEncode value is not the same, the following "Pythontab Chinese network" as an example :
# coding:utf-8str = U ' pythontab chinese net ' str = str.encode (' gb2312 ') d = {' name ': Str}q = Urllib.urlencode (d) Print Q
Results:
Name=pythontab%d6%d0%ce%c4%cd%f8
Note: The UrlEncode parameter must be dictionary
Other usage
Django is similar to UrlEncode in the following ways:
From django.utils.http Import Urlquotea = Urlquote (' pythontab Chinese net ') print a
Get the GBK code of Chinese characters
Urllib Converting strings
In fact, you can use the Urllib quote function to convert the Chinese in the URL, the Chinese to GBK encoding, the resulting encoding is the URL that conforms to the URI standard.
>>> import Urllib>>> a = "pythontab Chinese net" >>> a ' pythontab\xe4\xb8\xad\xe6\x96\x87\xe7\xbd\ X91 ' >>> urllib.quote (a) ' pythontab%e4%b8%ad%e6%96%87%e7%bd%91 ' >>>