浏览器整站下载工具

 1、下载安装nodejs,

2、npm安装模块 puppeteer-core,

3、然后运行当前js,参考demo输入url,就会下载所有的css、js、html等

 

//整站下载工具
const puppeteer = require('puppeteer-core');
const path = require('path');
//mkdir.js
const fs=require('fs');
const dirCache={};
function mkdir(filepath) {
    const arr=filepath.split('/');
    let dir=arr[0];
    for(let i=1;i<arr.length;i++){
        if(!dirCache[dir]&&!fs.existsSync(dir)){
            dirCache[dir]=true;
            fs.mkdirSync(dir);
        }
        dir=dir+'/'+arr[i];
    }
}//获取chrome浏览器第地址
const getDefaultOsPath = () => {
    if (process.platform === 'win32') {
        return 'C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe'
    } else {
        return '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome'
    }
};
const getUserDataDir = () => {
    if (process.platform === 'win32') {
        return 'D:\\User Data'
    } else {
        return '/Users/caoke/chromeUserdata'
    }
}
//睡眠
const sleep=function (time) {
    return new Promise(function (resolve) {
        setTimeout(resolve,time)
    })
};
//语法糖
async function runActions(arr){
    const page=this;
    for(let i=0;i<arr.length;i++){
        const action=arr[i];
        const key=action[0];
        if(key==='click'){
            page.click(action[1]);
        }else if(key==='keypress'){
            await page.keyboard.type(action[1]);
        }else if(key==='clickAndtype'){
            await page.type(action[1],action[2],{delay: 100});
        }else if(key==='type'){
            await page.keyboard.type(action[1],{delay: 100});
        }else if(key==='sleep'){
            await sleep(action[1])
        }else if(key==='uploadFile'){
            const elementHandle=await page.$(action[1]);
            await elementHandle.uploadFile(action[2])
        }else if(key==='goBack'){
            await page.goBack()
        }else  if(key==='userAgent'){
            await page.setUserAgent(action[1])
        }else if(key==='viewport'){
            await page.setViewport(action[1])
        }else if(key==='openUrl'){
            await page.goto(action[1]);
        }else if(key==='emulate'){
            await page.emulate(puppeteer.devices[action[1]]);
        }
    }


};
async function loadStatic(astionArr) {
    const browser = await puppeteer.launch({
        // headless: false,
        // ignoreDefaultArgs:true,
        // args :['--user-data-dir='+getUserDataDir()],
        executablePath:getDefaultOsPath(),
    });
    const page=await browser.newPage();
    page.on('response',async function (res) {
        if(!res){return;}
        try {
            const buffer=await res.buffer();
            if(!res.fromCache()&&buffer){
                const url=res.url();
                let filepath='./dist/'+url.replace(':/','').replace(/:/g,'').replace(/\?.+$/,'');
                mkdir(filepath);
                fs.writeFileSync(filepath,buffer);
            }
        }
        catch (e) {}
    })
    page.runActions=runActions;
    await page.runActions(astionArr);
    await browser.close();
}
module.exports=loadStatic;

//demo
loadStatic([
['openUrl','https://zhangyaochun.iteye.com/blog/2029910'],
['sleep',3000],
['openUrl','https://www.baidu.com'],
['sleep',3000],
])

下载nodejs,npm安装模块 puppeteer-core,然后运行当前js,参考demo输入url,就会下载所有的css、js、html等

转载于:https://www.cnblogs.com/caoke/p/11244916.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值