npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

fetch-html-img

v1.1.0

Published

fetch some imgs from html text

Readme

fetch-html-img

文档锚链接

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

1. 该库用来干什么?

通过html文本抓取图片

2. 配置描述

主配置

| 参数名 | 数据类型 | 必填|默认值 |简单描述 |举例| | :----:| :----: | :----: | :----: | :----: |:----: | | htmlStr | String|是🐢 | | 页面的html文本 |<html>...</html>| | selector | String|是🐢 | | 目标图片的css选择器 |".wrapper img"| | saveDir | String|是🐢 | | 保存图片的目录,需要传绝对路径 |path.resolve(__dirname , "./imgDir")| | host | String|是🐢 | | 抓取网站的地址(为了防止有些图片的src没有http字符串的情况) |"https://baidu.com"| | setImgName | Function|否⭕ | 默认是随机10个字符 | 设置生成的图片名称模板 |函数返回值就是生成的图片名称| | imgNum | Number|否⭕ | 9999 | 抓取的图片数量|| | sortRandom | Boolean|否⭕ | 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