栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何使用headless使用puppeteer下载文件:true?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何使用headless使用puppeteer下载文件:true?

此页面通过创建逗号分隔的字符串并通过设置数据类型来强制浏览器下载csv,从而下载csv

let uri = "data:text/csv;charset=utf-8," + enpreURIComponent(content);window.open(uri, "Some CSV");

chrome上的此按钮会打开一个新标签。

您可以点击此事件,然后将内容实际下载到文件中。不知道这是否是最好的方法,但是效果很好。

const browser = await puppeteer.launch({  headless: true});browser.on('targetcreated', async (target) => {    let s = target.url();    //the test opens an about:blank to start - ignore this    if (s == 'about:blank') {        return;    }    //unenpre the characters after removing the content type    s = s.replace("data:text/csv;charset=utf-8,", "");    //clean up string by unencoding the %xx    ...    fs.writeFile("/tmp/download.csv", s, function(err) {        if(err) { console.log(err); return;        }        console.log("The file was saved!");    }); });const page = await browser.newPage();.. open link ..... click on download link ..


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/406600.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号