node.js抓取网络图片保存到本地,node.js抓取防盗链网络图片保存到本地
使用模块request、fs,request模块在npm里平均月下载量超过3000W次,那是相当的牛逼。
node版本8.0+,如果版本是8.0以下调用的时候就直接用promise。
封装
common.jslet request = require("request");let fs = require("fs");class Ut {/*** 下载网络图片* @param {object} opts */static downImg(opts = {}, path = '') {return new Promise((resolve, reject) => {request.get(opts).on('response', (response) => {console.log("img type:", response.headers['content-type'])}).pipe(fs.createWriteStream(path)).on("error", (e) => {console.log("pipe error", e)resolve('');}).on("finish", () => {console.log("finish");resolve("ok");}).on("close", () => {console.log("close");})})};}module.exports = Ut;
测试普通网络图片
test.js 下载csdn自己的头像let Ut = require("./common");(async () => {try {let url = "/1/A/1/3_zzwwjjdj1.jpg";let opts = {url: url,};let path = "./1.jpg";let r1 = await Ut.downImg(opts, path);console.log(r1);}catch (e) {console.log(e);}})()
运行
node test.js测试防盗链图片
/thumbs//01/117406_24c17_236.jpglet Ut = require("./common");(async () => {try {let url = "/thumbs//01/117406_24c17_236.jpg";let opts = {url: url,};let path = "./2.jpg";let r1 = await Ut.downImg(opts, path);console.log(r1);}catch (e) {console.log(e);}})()
执行node test.js得到的不是原图,原图是个妹子图片
这个时候就要分析原网站防盗链的方法,这个网站需要在头部加上 Referer:/
let Ut = require("./common");(async () => {try {let url = "/thumbs//01/117406_24c17_236.jpg";let opts = {url: url,headers: {'Referer': '/',}};let path = "./2.jpg";let r1 = await Ut.downImg(opts, path);console.log(r1);}catch (e) {console.log(e);}})()
再次运行node test.js 就会得到原图
完成。