You can retrieve a list of all links on a page using Puppeteer by following these steps:
npm install puppeteer
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('https://example.com'); // Replace 'https://example.com' with the URL of the webpage you want to extract links from
// Extract all links on the page
const links = await page.evaluate(() => {
const allLinks = document.querySelectorAll('a');
const linksArray = Array.from(allLinks);
return linksArray.map(link => link.href);
});
console.log(links);
await browser.close();
})();
node your_script_name.js
This code snippet uses Puppeteer to launch a headless browser, navigate to a specified webpage, and extract all links (URLs) present on the page. The extracted links are then logged to the console.