使用nodeJS下载图片

javascript 复制代码
const http = require('http');
const https = require("https");
const cheerio = require('cheerio');
const request = require('request');
const fs = require('fs');

function filterData(data) {
    const $ = cheerio.load(data);
    let textLi = '';
    $('.goods-list-wrap .gl-item img.item-pic').each((index, el) => {
        let imgurl = $(el).attr('data-original');
        console.log($(el).attr('data-original'))
        textLi += $(el).attr('data-original') + "\n";
        console.log(imgurl.split('/')[imgurl.split('/').length - 1], 'fhg')
        let fileName = "./img/" + imgurl.split('/')[imgurl.split('/').length - 1];
        download_img('https:' + imgurl, fileName)
    })
    console.log($('#title-category').text(), 'text')
    fs.writeFile(`${$('#title-category').text()}.txt`, textLi, (err) => {
            if (err) throw err;
            console.log('文本文件已创建并写入数据');
        })
        // console.log(data);
}

// 下载图片
function download_img(img_url, file_name) {
    request(img_url).pipe(fs.createWriteStream(file_name)).on('close', function() {
        console.log('pic saved!')
    })
}
const server = http.createServer((req, res) => {
    let data = "";
    https.get("https://lists.meizu.com/page/list?categoryid=76", (result) => {
        result.on("data", (chuck) => {
            data += chuck;
        })

        result.on("end", () => {
            filterData(data);
        })
    })
})


server.listen(8080, () => {
    console.log("locallhost:8080")
})
相关推荐
喜欢踢足球的老罗1 天前
PM2 进程持久化实战:深度解析 save 与 startup 的协同机制
nodejs·pm2
John Song12 天前
npx 与 npm 的区别
npm·nodejs
belldeep1 个月前
nodejs v18.20 如何使用 express markdown-it 和 mermaid.min.js 10.9
nodejs·express·markdown·mermaid
ayuday1 个月前
Svelte - 现代高性能Web开发框架!轻量级‌、响应式、编译时优化‌特点
nodejs·svelte
Wang's Blog1 个月前
Nodejs-HardCore: 流类型、应用与内置类型实战
nodejs
Wang's Blog1 个月前
Nodejs-HardCore: 玩转 EventEmitter 指南
开发语言·nodejs
winfredzhang1 个月前
自动化从文本到目录:深度解析 Python 文件结构管理工具
python·ai·nodejs·文件结构
Wang's Blog1 个月前
Nodejs-HardCore: Buffer操作、Base64编码与zlib压缩实战
开发语言·nodejs
Wang's Blog2 个月前
Nodejs-HardCore: 深入解析DBF文件之二进制文件处理指南
开发语言·nodejs
云霄IT2 个月前
[最新可用]centos7安装Node.js版本v21.5.0和pm2管理工具
nodejs