1.1.0 • Published 3 years ago

fetch-html-img v1.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

fetch-html-img

文档锚链接

1. 该库用来干什么?
2. 配置描述
3. Installation安装
4. Usage使用案例

1. 该库用来干什么?

通过html文本抓取图片

2. 配置描述

主配置
参数名数据类型必填默认值简单描述举例
htmlStrString是🐢页面的html文本\...\
selectorString是🐢目标图片的css选择器".wrapper img"
saveDirString是🐢保存图片的目录,需要传绝对路径path.resolve(__dirname , "./imgDir")
hostString是🐢抓取网站的地址(为了防止有些图片的src没有http字符串的情况)"https://baidu.com"
setImgNameFunction否⭕默认是随机10个字符设置生成的图片名称模板函数返回值就是生成的图片名称
imgNumNumber否⭕9999抓取的图片数量
sortRandomBoolean否⭕false是否乱序抓取页面上的图片

3. Installation安装

npm install fetch-html-img
或者
yarn add fetch-html-img

4. Usage使用案例

1.安装库
npm install fetch-html-img
或者
yarn add fetch-html-img
2.创建data.html文件,并把目标页面的html文本拷贝至data.html
3.创建fetchImg.js文件
const fs = require("fs");
const fetchImg = require("fetch-html-img")
const path = require("path")
let res =  fs.readFileSync(path.resolve("./data.html"));
res = res.toString("utf-8");
fetchImg({
    htmlStr: res,//html文本
    selector: ".imgitem .main_img",
    saveDir: path.resolve(__dirname, "./qq"),
    host: "https://www.baidu.com",
    imgNum: 5,
    sortRandom: false,
    setImgName: function(imgUrl , index){
        /* imgUrl是请求的图片名,如:http://xxx/aaa.jpg
            index是图片索引
        */
        let targetImgName = `${index}-${Math.random().toString(16).substr(2, 5)}${Math.random().toString(16).substr(2, 5)}`;
        let ext = path.extname(imgUrl);
        if (!ext) {
            ext = ".jpg"
        }
        targetImgName = `${targetImgName}${ext}`;
        return targetImgName;
    }
});
3.通过nodejs运行fetchImg.js文件(控制台输出:抓取结束才算结束)
node fetchImg.js
1.1.0

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago