引用了两个第三方插件
- superagent 一个轻量级、灵活的客户端请求代理模块
- cheerio nodejs的jq
简单的代码
const superagent = require('superagent')
const cheerio = require("cheerio")
const fs = require("fs")
superagent.get('https://www.acfun.cn/v/list123/index.htm').then(res => {
const $ = cheerio.load(res.text)
let el = $('body').find('img')
el.map((index, i) => {
let img_src = $(i).attr('data-original') || $(i).attr('src'),
news_title = $(i).attr('alt') || ''
if (!img_src) return
superagent.get(img_src).pipe(fs.createWriteStream('./image/' + index + '.jpg'));
})
}, function (error) {
console.log(error)
})
思路
使用superagent访问网站,cheerio解析网页后拿到我们想要的东西
再使用superagent拿到文件流,最后写入到文件中
提醒
部分网站存在限制,可自行尝试
简单的防御可以通过文中set方法添加header等
github:github.com/CHU295/node…