美文网首页
http模块的使用

http模块的使用

作者: JohnYuCN | 来源:发表于2020-04-20 14:20 被阅读0次

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();

相关文章

网友评论

      本文标题:http模块的使用

      本文链接:https://www.haomeiwen.com/subject/zzwqpctx.html