Beautifulsoup encoding (Zhuan)

Source: Internet
Author: User
Beautifulsoup Encoding

Beautifulsoup uses Unicode internally. beautifulsoup automatically detects the encoding type of the input file and converts it to Unicode.

Beautifulsoup encoding detection sequence

Beautifulsoup:

    1. Fromencoding parameter passed during soup object creation;
    2. Encoding defined by the XML/html file;
    3. The encoding feature represented by several bytes in the file. The encoding that can be judged at this time can only be one of the following encodings: UTF-*, ebcdic, and ASCII.
    4. If chardet is installed, beautifulsoup uses chardet to detect file encoding.
    5. UTF-8
    6. Windows-1252

In the beautifulsoup source file, there is such a sentenceCode:

 
Default_output_encoding ="UTF-8"

Note that beautifulsoup uses UTF-8 encoding by default.

Encoding handler in beautifulsoup

The originalencoding variable in beautifulsoup provides the file Encoding Detected by beautifulsoup.

Import Urllib2FromBeautifulsoupImportBeautifulsoup Doc =Urllib2.Urlopen("Http://www.pythonclub.org /")Soup = beautifulsoup(Doc)Soup.Originalencoding# U'utf-8'
Beautifulsoup processes Chinese Encoding

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.