How to download file with puppeteer using headless: true?

前端 未结 7 1690
攒了一身酷
攒了一身酷 2020-12-08 05:05

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

7条回答
  •  一生所求
    2020-12-08 05:48

    I spent hours poring through this thread and Stack Overflow yesterday, trying to figure out how to get Puppeteer to download a csv file by clicking a download link in headless mode in an authenticated session. The accepted answer here didn't work in my case because the download does not trigger targetcreated, and the next answer, for whatever reason, did not retain the authenticated session. This article saved the day. In short, fetch. Hopefully this helps someone else out.

    const res = await this.page.evaluate(() =>
    {
        return fetch('https://example.com/path/to/file.csv', {
            method: 'GET',
            credentials: 'include'
        }).then(r => r.text());
    });
    

提交回复
热议问题