2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Puppeteer(パペティア)でcsvファイルをダウンロードする

Posted at

概要

ずっとやりたかったPuppeteerでファイルダウンロードをやってみた。
世の中いろんなやり方が書かれていて混乱したが、やってみたら簡単にできたので共有。

参考 - 公式issue
https://github.com/puppeteer/puppeteer/issues/299

環境

  • Ubuntu18.04
  • Node.js v14.15.1
  • Puppeteer 8.0.0

ソース

意外と普通にできました!
何か罠が待っている気もしなくもなく。

const puppeteer = require('puppeteer');
const fs = require('fs');
const path = require("path");

(async () => {
    const browser = await puppeteer.launch();
    const page = await browser.newPage();
    const width = 1980;
    const height = 1080;
    await page.setViewport({ width, height });
    await page.goto('https://portal.local/');
    // gotoの後は、waitしなくてよさげ

    // ファイルダウンロード
    const downloadPath = path.resolve('/hoge/fuga/'); // ここは絶対に絶対パス
    await page._client.send('Page.setDownloadBehavior', {
        behavior: 'allow',
        downloadPath: downloadPath 
    });

    // csvダウンロードボタンクリック
    await page.click('.csvDownload');
    await page.waitForTimeout(5000);

    await browser.close();
})();

参考

2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?