- 自動のセッション管理
- 全世界195ヵ国の各都市がターゲット対象
- 無制限の同時セッション
How to Download a File With Puppeteer?
Downloading files with Puppeteer is straightforward. Below is an example of how to do it efficiently without unnecessary complexity.
Here, we will automate the download of a file by setting the download behavior and specifying the download path.
const puppeteer = require('puppeteer');
const path = require('path');
(async () => {
const downloadPath = path.resolve('./download'); // Define download path
const browser = await puppeteer.launch({ headless: true });
const page = await browser.newPage();
// Set download behavior and path
await page._client.send('Page.setDownloadBehavior', {
behavior: 'allow',
downloadPath: downloadPath
});
await page.goto('https://example.com/download', { waitUntil: 'networkidle2' });
// Click on the download button
await page.click('#downloadButton'); // Adjust the selector as needed
console.log('File download initiated.');
await browser.close();
})();
Explanation:
- Download Path: Set the path where the file will be downloaded using
path.resolve
. - Browser Launch: Initiate a headless browser instance.
- Set Download Behavior: Configure Puppeteer to allow downloads and specify the download directory.
- Navigate to URL: Go to the webpage containing the download link.
- Simulate Click: Click the download button to initiate the file download.
For more advanced file handling, you can refer to Bright Data’s Scraping Browser, which offers enhanced capabilities like automatic CAPTCHA solving and handling complex website interactions, making it ideal for extensive web scraping projects.
20,000+ 人以上のお客様に世界中で信頼されています
20,000+ 人以上のお客様に世界中で信頼されています
細部まで 調整可能で、非常に効率よく動作
Scraping Cloudへようこそ