如何使用headless使用puppeteer下载文件:true? [英] How to download file with puppeteer using headless: true?

查看:358
本文介绍了如何使用headless使用puppeteer下载文件:true?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在运行以下代码,以便从网站 http://niftyindices.com/resources/下载 csv 文件假日日历

I've been running the following code in order to download a csv file from the website http://niftyindices.com/resources/holiday-calendar:

const puppeteer = require('puppeteer');

(async () => {
const browser = await puppeteer.launch({headless: true});
const page = await browser.newPage();

await page.goto('http://niftyindices.com/resources/holiday-calendar');
await page._client.send('Page.setDownloadBehavior', {behavior: 'allow', 
downloadPath: '/tmp'})
await page.click('#exportholidaycalender');
await page.waitFor(5000);
await browser.close();
})();

带有 headless:false 将文件下载到 / Users / user / Downloads headless:true 没用。

with headless: false it works, it downloads the file into /Users/user/Downloads. with headless: true it does NOT work.

我正在macOS Sierra(MacBook Pro)上运行它使用puppeteer版本 1.1.1 将Chromium版本 66.0.3347.0 拉入 .local-chromium / 目录,并使用 npm init npm i-保存操纵符进行设置。

I'm running this on a macOS Sierra (MacBook Pro) using puppeteer version 1.1.1 which pulls Chromium version 66.0.3347.0 into .local-chromium/ directory and used npm init and npm i --save puppeteer to set it up.

有什么问题吗?

在此先感谢您的时间和帮助,

Thanks in advance for your time and help,

推荐答案

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

This page downloads a csv by creating a comma delimited string and forcing the browser to download it by setting the data type like so

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

在chrome上这会打开一个新标签。

This on chrome opens a new tab.

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

You can tap into this event and physically download the contents into a file. Not sure if this is the best way but works well.

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;
    }
    //unencode 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 ..

这篇关于如何使用headless使用puppeteer下载文件:true?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆