Urlencode: The urlencode parameter must be Dictionaryimporturllibd {& amp; 39; name1 & amp; amp; 39;: & amp; 39; wwwpythontabcom & amp; 39;, & amp; 39; name2 &
Urlencode call 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
It is equivalent to splicing two url parameters. this usage is similar to http_build_query () in PHP. here, we won't know how to use it in PHP. if you are interested, check it yourself.
Urlencode encoding
The urlencode function does not change the original encoding of input parameters. that is to say, you need to adjust the encoding of post or get parameters before calling.
Problem: Now we simulate the request for Google and baidu. because baidu uses gb2312 encoding and google uses utf8 encoding, the urlencode values of the Chinese parameters submitted to the URL by the two sites are different, the following uses "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
Result:
name=PythonTab%D6%D0%CE%C4%CD%F8
Note: The urlencode parameter must be a Dictionary
Other usage
Urlencode in django is similar to the following method:
From django. utils. http import urlquotea = urlquote ('pythontab Chinese net') print
Obtain the GBK encoding of Chinese characters
Urllib conversion string
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 = "PythonTab 文"" >>> a' PythonTab \ xe4 \ xb8 \ xad \ xe6 \ x96 \ x87 \ xe7 \ xbd \ x91 '>> urllib. quote (a) 'pythontab % E4 % B8 % AD % E6 % 96% 87% E7 % BD % 91 '>