安装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')+"------");
})
});
});

结果如下:

最新文章

  1. 匈牙利算法(codevs2776)
  2. c++实现gray code(格雷码)
  3. Prince2七大原则(3)
  4. 如何克隆路由器MAC地址,怎么操作?
  5. white-space:nowrap 的妙用
  6. 1.shell之搭建Shell编程环境
  7. BZOJ 2176 Strange String (最小表示法)
  8. javascript语言精粹:继承
  9. Jeditable 点击编辑文字插件
  10. tempnam问题
  11. 几种加密算法的java实现包括MD5、RSA、SHA256
  12. uboot中 make xxx_config 的作用(以make smdk2410_config为例)
  13. PHP性能优化利器:生成器 yield理解
  14. Nginx开启gzip压缩解决react打包文件过大
  15. ForkJoinPool
  16. CSDN日报20170226——《你离心想事成仅仅差一个计划》
  17. [LintCode] Subarray Sum & Subarray Sum II
  18. Express框架中如何引用ejs模板引擎
  19. Java基础-包(package)的声明与访问
  20. 每日一条 git 命令行:git clone https://xxxxx.git -b 12.0 --depth 1

热门文章

  1. linux命令行操作mysql数据库明细
  2. c++容器中map的应用
  3. InnoDB: Error number 24 means ‘Too many open files’
  4. KOA 学习(四)
  5. virtualenv简单使用
  6. 2018-10-31-C#-程序内的类数量对程序启动的影响
  7. Leetcode492.Construct the Rectangle构造矩形
  8. JavaScript中[]+[] 、[]+{}、{}+[]、{}+{}的结果分析
  9. python基础--常用的模块(collections、time、datetime、random、os、sys、json、pickle)
  10. java-面向对象-封装-this-构造函数