Python obtains the webpage encoding implementation code, and python Encoding

Source: Internet
Author: User

Python obtains the webpage encoding implementation code, and python Encoding

Python code for obtaining webpage Encoding

<Span style = "font-family: Arial, Helvetica, sans-serif; background-color: rgb (255,255,255 ); "> </span> <span style =" font-family: Arial, Helvetica, sans-serif; background-color: rgb (255,255,255); "> python development, the chardet library and Character Set detection are used to automatically obtain webpage encoding. This class is not found in python2.7 and needs to be downloaded from the official website. Now I have downloaded the chardet-2.3.0.tar.gz package file. You only need to put the chardet file decompressed by the package file in python27/lib/site-packages/Under the python installation package. </Span>

Then import chardet

The following describes an automatic detection function for detecting Url connections and then returns the url encoding method of the webpage.

Import chardet # Character Set detection import urllib url = "http://www.jd.com" def automatic_detect (url): content = urllib. urlopen (url ). read () result = chardet. detect (content) encoding = result ['encoding'] return encoding urls = ['HTTP: // www.baidu.com ', 'HTTP: // www.163.com', 'HTTP: // dangdang.com '] for url in urls: print url, automatic_detect (url)

The detect method of the chardet class is used above, the dictionary is returned, and the encoding method encoding is taken out.

Thank you for reading this article. I hope it will help you. Thank you for your support for this site!

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.