Node. js crawls garbled Chinese webpages and solutions _ node. js

Source: Internet
Author: User
This article mainly introduces Node. this article describes how to use some open-source libraries to solve the garbled problem during crawling. For more information, see Node. garbled characters may occur when JavaScript captures a non-UTF-8 Chinese webpage. For example, if Netease's homepage code is gb2312, garbled characters may occur during crawling.

The Code is as follows:


Var request = require ('request ')
Var url = 'HTTP: // www.163.com'

Request (url, function (err, res, body ){
Console. log (body)
})


You can use iconv-lite to solve this problem.

Install

The Code is as follows:


Npm install iconv-lite


At the same time, we can modify the user-agent to prevent website blocking:

The Code is as follows:


Var originRequest = require ('request ')
Var iconv = require ('iconv-lite ')
Var headers = {
'User-agent': 'mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.65 Safari/8080'
}

Function request (url, callback ){
Var options = {
Url: url,
Encoding: null,
Headers: headers
}
OriginRequest (options, callback)
}

Request (url, function (err, res, body ){
Var html = iconv. decode (body, 'gb2312 ')
Console. log (html)
})

Garbled Problem Solving

Use cheerio to parse HTML

Cheerio can be simply and roughly understood as the jQuery selector on the server side. It is much more intuitive than regular expressions.

Install

The Code is as follows:


Npm install cheerio
Request (url, function (err, res, body ){
Var html = iconv. decode (body, 'gb2312 ')
Var $ = cheerio. load (html)
Console. log ($ ('h1 '). text ())
Lele.log((('h1'{.html ())
})


Output:

The Code is as follows:


Netease
Netease


As a matter of question, the code output by ('h1'{.html () is Unicode encoded, and Netease has become Netease, which brings us some trouble in character processing.

Solve the "garbled" problem of cheerio. html ()
You can disable this function to convert entity encoding.

The Code is as follows:


Var $ = cheerio. load (html)


Change

The Code is as follows:


Var $ = cheerio. load (html, {decodeEntities: false })


The complete code is as follows:

The Code is as follows:


Var originRequest = require ('request ')
Var cheerio = require ('cheerio ')
Var iconv = require ('iconv-lite ')
Var headers = {
'User-agent': 'mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.65 Safari/8080'
}

Function request (url, callback ){
Var options = {
Url: url,
Encoding: null,
Headers: headers
}
OriginRequest (options, callback)
}

Var url = 'HTTP: // www.163.com'

Request (url, function (err, res, body ){
Var html = iconv. decode (body, 'gb2312 ')
Var $ = cheerio. load (html, {decodeEntities: false })
Console. log ($ ('h1 '). text ())
Lele.log((('h1'{.html ())
})

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.