cherrio模块

安装
cnpm install cherrio
使用方法
const cheerio = require('cheerio')
const $ = cheerio.load('<h2 class="title">Hello world</h2>') $('h2.title').text('Hello there!')
$('h2').addClass('welcome') $.html()
//=> <html><head></head><body><h2 class="title welcome">Hello there!</h2></body></html>

request模块

var request = require('request');
request('http://www.google.com', function (error, response, body) {
console.log('error:', error); // Print the error if one occurred
console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
console.log('body:', body); // Print the HTML for the Google homepage.
});

爬虫系统 request cheerio

  1. 爬取一个网站的内容信息
  2. 分析内容
  3. 储存数据 下载图片

简单的爬虫

//发起服务端请求 请求一个网页
const request = require('request')
const fs= require('fs')
const path= require('path')
const cheerio = require('cheerio')
//以百度为例
let url ='https://www.baidu.com/'
request(url,(err,response,body)=>{
console.log(err)
//把爬取到的body 写入新文件中
fs.writeFile(path.join(__dirname,'./baidu.html'),body,(err)=>{
if(err){
console.log('爬取失败')
}else{
console.log('爬取成功')
}
})
//根据一个网址 下载对应的网页文件
const $ = cheerio.load(body)
let imgs = []
// 用正则判断数组中的路径是否存在https
var Reg = /(http[s]?|ftp)/;
$('img').each((index, ele) => { // 遍历所有
var src = $(e).attr('src');
if (!Reg.test(src)) {
src = src.replace(/\/{2}/, 'https://') //正则判断
}
imgs.push(src)
})
// 下载数组里的图片
for (let index = 0; index < imgs.length; index++) {
if (imgs[index].indexOf('png') !== -1) {
request(imgs[index]).pipe(fs.createWriteStream(`./img/${index}.png`)) //用下标命名,要建好img文件夹
}; }
})

最新文章

  1. python多种格式数据加载、处理与存储
  2. 1125MySQL Sending data导致查询很慢的问题详细分析
  3. Memcached和Memcache安装(64位win7)
  4. Mongodb 资源
  5. BIEE 11g 安装
  6. 解决LinearLayout中控件不能居右对齐
  7. CoffeeScript学习(2)—— 变量
  8. Oracle EBS-SQL (CST-1):检查BOM历史成本查询(Average Cost).sql
  9. 运用JavaScript构建你的第一个Metro式应用程序(on Windows 8)(一)
  10. java实现树型结构样式
  11. 开发 chrome 扩展 GitHub-Remarks 的一些想法以及遗憾
  12. C# 将前端传来的图片文件分别以大图和缩略图保存
  13. Linux无法正常连接服务器,无法连接上 127.0.0.1:8989 (127.0.0.1)。 - connect (111: 拒绝连接)
  14. 每日一练ACM
  15. ubuntu 16.04 清空log文件的方法
  16. leetCode-linkedListCycle判断链表是否有环
  17. leetcode 数组类型题
  18. linux ubuntu 18.04 无线网卡 rtl8821ce的安装
  19. JSON文件导入Unity3d中是空的的问题
  20. How to Build a Search Page with Elasticsearch and .NET

热门文章

  1. 使用内存映射文件MMF实现大数据量导出时的内存优化
  2. phpstudy nginx设置CORS跨域不起作用的可能解决方法
  3. 【一起学源码-微服务】Hystrix 源码一:Hystrix基础原理与Demo搭建
  4. Gitlab安装配置管理
  5. nginx介绍与安装
  6. select的disabled形式的数据,使用表单序列化方式无法将数据传到后台
  7. 8.for循环及练习
  8. 为什么你应该使用 Kubernetes(k8s)
  9. Java单体应用 - 开发工具 - 01.IntelliJ IDEA
  10. 解决谷歌浏览器设置font-family属性不起作用,(css中设置了font-family:没有用)css字体属性没用