Nodejs captures non-utf8 character encoding pages

Source: Internet
Author: User
Problem: nodejs cannot process non-utf8 characters at present

In the cnodejs user group, some people encountered code problems when capturing Baidu pages.

In buffer. tostring (encoding), encoding only supports utf8 encoding. Therefore, an additional module is required to handle this problem.

Solution: node-iconv Module

Installation:

$ NPM install iconv
Example
VaR HTTP = require ('http'); var Options = {Host: 'www .baidu.com ', Port: 80, path:'/s? WD = nodejs '}; var iconv = require ('iconv '). iconv; http. get (options, function (RES) {console. log ("got response:" + Res. statuscode, Res. headers); var buffers = [], size = 0; Res. on ('data', function (buffer) {buffers. push (buffer); size + = buffer. length ;}); Res. on ('end', function () {var buffer = new buffer (size), Pos = 0; For (VAR I = 0, L = buffers. length; I

The specific page encoding can be determined based on res. headers ['content-type.
If res. headers ['content-type'] is not available, you need to analyze the Content-Type of HTML to determine charset.

"{Meta http-equiv =" Content-Type "content =" text/html; charset = XXXX "/}"

For more URL-related requests, use the urllib library.

Love

Hope this article will be useful to you ^_^

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.