expediagroup.github.io
Version:
53 lines (43 loc) • 1.68 kB
JavaScript
const axios = require('axios');
const cheerio = require('cheerio');
const fs = require('fs');
const path = require('path');
// Function to download a file
async function downloadFile(url, destination) {
const writer = fs.createWriteStream(destination);
const response = await axios({
url,
method: 'GET',
responseType: 'stream'
});
response.data.pipe(writer);
return new Promise((resolve, reject) => {
writer.on('finish', resolve);
writer.on('error', reject);
});
}
// Function to scrape links from a webpage and download files
async function scrapeAndDownload(url) {
try {
const response = await axios.get(url);
const $ = cheerio.load(response.data);
// Iterate over all <a> tags and extract the href attribute
$('a').each(async (index, element) => {
const href = $(element).attr('href');
// Check if it's a link to a file
if (href && href.startsWith('http')) {
// Determine the file name from the URL
const fileName = path.basename(href);
console.log(`Downloading ${fileName} from ${href}`);
// Download the file
await downloadFile(href, fileName);
console.log(`${fileName} downloaded successfully!`);
}
});
} catch (error) {
console.error('Error:', error);
}
}
// Call the function with the URL of the webpage you want to scrape
const webpageUrl = 'https://example.com'; // Change this to the desired webpage URL
scrapeAndDownload(webpageUrl);