nodejs小爬虫demo

    技术2022-07-10  108

    var http = require('http'); var https = require('https'); // cnpm i -S cheerio var cheerio = require('cheerio'); //发起一个get请求 //http.get(url) https.get('https://www.lagou.com/',(req)=>{ var res = ''; req.on('data',(chunk)=>{ //事件,可以分批次爬到数据 // console.log( chunk ) //爬到的部分数据 res += chunk; }) req.on('end',function(){ //事件, 读取结束后触发一次 // console.log(res) getData( res ); }) }); function getData(html){ var $ = cheerio.load(html); $('.menu_box h2').each((i,elem)=>{ console.log( $(elem).text().trim() ); }) }
    Processed: 0.020, SQL: 9