Python implements url encoding for Chinese conversion

Source: Internet
Author: User
Tags keyword list
This article mainly introduces how to implement url encoding for converting Chinese characters in python, and analyzes the skills related to the conversion of gbk and UTF-8 encoding for Chinese characters in combination with examples, which has some reference value, for more information about how to convert Chinese characters into url encoding, see the example in this article. We will share this with you for your reference. The details are as follows:

Today, I want to deal with Baidu posts. If you want to create a keyword list, you can directly add it to the list whenever you need it. However, the url is encoded as '% E4 % B8 % BD % E6 % B1 % 9f' when Chinese characters are added to the list (for example, 'lijiang ', therefore, a conversion is required. Here we use the urllib module.

>>> Import urllib >>> data = 'lijiang '>>> print data Lijiang >>> data \ xe4 \ xb8 \ xbd \ xe6 \ xb1 \ x9f' >>> urllib. quote (data) '% E4 % B8 % BD % E6 % B1 % 9f'

So we want to go back?

>>> Urllib. unquote ('% E4 % B8 % BD % E6 % B1 % 9f')' \ xe4 \ xb8 \ xbd \ xe6 \ xb1 \ x9f'> print urllib. unquote ('% E4 % B8 % BD % E6 % B1 % 9f') Lijiang

Students will find that % C0 % F6 % BD % AD appears in the post bar url, not '% E4 % B8 % BD % E6 % B1 % 9f ', it is actually a coding problem. Baidu is gbk, and other general websites such as google are utf8. Therefore, you can use the following statements.

>>> Import sys, urllib >>> s = 'lijiang '>>> urllib. quote (s. decode (sys. stdin. encoding ). encode ('gbk') '% C0 % F6 % BD % ad' >>> urllib. quote (s. decode (sys. stdin. encoding ). encode ('utf8') '% E4 % B8 % BD % E6 % B1 % 9f' >>>

For more articles about how to implement url encoding for Chinese conversion in python, refer to the PHP Chinese website!

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.