node 写的简单爬虫(一)
2024-10-08 01:31:10
安装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')+"------");
})
});
});
结果如下:
最新文章
- 匈牙利算法(codevs2776)
- c++实现gray code(格雷码)
- Prince2七大原则(3)
- 如何克隆路由器MAC地址,怎么操作?
- white-space:nowrap 的妙用
- 1.shell之搭建Shell编程环境
- BZOJ 2176 Strange String (最小表示法)
- javascript语言精粹:继承
- Jeditable 点击编辑文字插件
- tempnam问题
- 几种加密算法的java实现包括MD5、RSA、SHA256
- uboot中 make xxx_config 的作用(以make smdk2410_config为例)
- PHP性能优化利器:生成器 yield理解
- Nginx开启gzip压缩解决react打包文件过大
- ForkJoinPool
- CSDN日报20170226——《你离心想事成仅仅差一个计划》
- [LintCode] Subarray Sum &; Subarray Sum II
- Express框架中如何引用ejs模板引擎
- Java基础-包(package)的声明与访问
- 每日一条 git 命令行:git clone https://xxxxx.git -b 12.0 --depth 1
热门文章
- linux命令行操作mysql数据库明细
- c++容器中map的应用
- InnoDB: Error number 24 means ‘Too many open files’
- KOA 学习(四)
- virtualenv简单使用
- 2018-10-31-C#-程序内的类数量对程序启动的影响
- Leetcode492.Construct the Rectangle构造矩形
- JavaScript中[]+[] 、[]+{}、{}+[]、{}+{}的结果分析
- python基础--常用的模块(collections、time、datetime、random、os、sys、json、pickle)
- java-面向对象-封装-this-构造函数