zoukankan      html  css  js  c++  java
  • node 写的简单爬虫(一)

    安装cheerio

    npm install cheerio --save

    引入http和cheeri

    var http=require("http");
    var cheerio=require("cheerio");

    1.爬取新闻

    我们选择新浪新闻来进行爬取

    http://news.sina.com.cn/china/
    http.get(url,function(res){
         var html='';
         res.on('data',function(data){
             html +=data
         })
     
         res.on('end', function() {
             var $=cheerio.load(html);
            $("#subShowContent2_static .news-item h2").each((iten,i)=>{
                console.log($(i).text());
            })
          console.log("数据加载完毕");
         });
     }).on('error', function() {
         console.log("获取数据出错!")
     });

    结果如下:

    2.爬取图片

    我们选择天极网的图片进行爬取

    http://pic.yesky.com/
    http.get(url, function (res) {
            var imageData ='';
            res.on('data',function(data){  //图片加载到内存变量
                imageData += data;
            }).on('end',function(){        //图片加载完
                var $=cheerio.load(imageData);
                $Imgs = $('img'),
                $Imgs.each((iten,i)=>{
                console.log($(i).attr('src')+"------");
               })   
            });
        });

    结果如下:

    记录生活中的点点滴滴!
  • 相关阅读:
    消息中间件
    线程以及多线程
    锁以及分布式锁
    并发以及高并发
    SpringBoot + SpringCloud学习踩坑实记
    公众号笔记: 2018年12月
    浅谈final关键字的用法
    浅谈static关键字的四种用法
    Linux常用的一些命令
    HTTPS
  • 原文地址:https://www.cnblogs.com/aSnow/p/8438425.html
Copyright © 2011-2022 走看看