Python crawls and saves html pages with garbled characters.

Source: Internet
Author: User

Python crawls and saves html pages with garbled characters.

This example describes how to solve the garbled problem when python crawls and saves html pages. We will share this with you for your reference. The details are as follows:

When crawling html pages and saving them with Python, the contents of captured webpages are often garbled. The cause of this problem is that the encoding settings in your code are incorrect. On the other hand, the actual encoding and the labeled encoding of the webpage are incorrect when the encoding settings are correct. The encoding of the html page is as follows:
Copy codeThe Code is as follows: <meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>

Here is a simple solution: Use chardet to determine the real code of the webpage, and determine the flag encoding from the info returned by the url request. If the two encoding types are different, the bs module is extended to GB18030 encoding. If the two encoding types are the same, files are written directly (the default encoding is UTF-8 here ).

import urllib2import sysimport bs4import chardetreload(sys)sys.setdefaultencoding('utf-8')def download(url):  htmlfile = open('test.html','w')  try:    result = urllib2.urlopen(url)    content = result.read()    info = result.info()    result.close()  except Exception,e:    print 'download error!!!'    print e  else:    if content != None:      charset1 = (chardet.detect(content))['encoding'] #real encoding type      charset2 = info.getparam('charset') #declared encoding type      print charset1,' ', charset2      # case1: charset is not None.      if charset1 != None and charset2 != None and charset1.lower() != charset2.lower():        newcont = bs4.BeautifulSoup(content, from_encoding='GB18030')  #coding: GB18030        for cont in newcont:          htmlfile.write('%s\n'%cont)      # case2: either charset is None, or charset is the same.      else:        #print sys.getdefaultencoding()        htmlfile.write(content) #default coding: utf-8  htmlfile.close()if __name__ == "__main__":  url = 'http://www.bkjia.com'  download(url)

Test.html file to open the following, you can see that the use of UTF-8 without BOM encoding format storage, that is, we set the default encoding:

Related Article

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.