Python urlencode Encoding

Source: Internet
Author: User

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' >>>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.