http.ClientRequest结合http.Agent,cheerio的使用
const http = require('http');
const cheerio=require('cheerio')
const agent = new http.Agent({
keepAlive: true,
keepAliveMsecs: 1000,
maxSockets: 4,
maxFreeSockets: 2
});
const option = {
protocol: 'http:',
host: '127.0.0.1',
port: 8080,
path: `/test1.html`,
agent: agent,
// agent: agent,
headers: {"Connection": "keep-alive"},
method: 'GET'
};
const req = http.request(option, function(res) {
res.setEncoding('utf8');
let body = '';
res.on('data', (chunk) => {
body += chunk;
});
res.on('end', () => {
const $ = cheerio.load(body,{xml:{normalizeWhitespace: true}});
$('li').each(function (index,element) {
console.log($(element).text())
})
});
});
req.end();
网友评论