先要装jsdom: npm install jsdom.
var jsdom = require('jsdom');
const { JSDOM } = jsdom;
var http = require('http');
var html = [];
var request = http.get(pageUrl,function(res){
res.on('data',function(data){
html.push(data);
});
res.on('end',function(){
gbkBufferToUtf8Buffet(html,function(utf8String){
var item = new JSDOM(utf8String);
var document = item.window.document;
analyzeHTMLDListElementList(document.getElementsByClassName('xi2'),function(){});
});
});
});
request.on('error',function(error){
console.log('error is ' + error);
});
request.end();
/*
查找HTML元素
(1)利用getElementById()方法
(2)利用getElementsByTagName()方法
(3)利用getElementsByClassName()方法
(4)利用getElementsByName()方法
这里有更多的使用:http://www.runoob.com/jsref/dom-obj-document.html
*/
//GBK转码
function gbkBufferToUtf8Buffet(gbkBuffer,callBackFunction){
var iconv = require('iconv-lite');
var decodedBody = iconv.decode(Buffer.concat(gbkBuffer), 'GBK');
var string = decodedBody.toString();
callBackFunction(string);
}