- Автоматическое управление сеансами
- Таргетинг на любой город в 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