How to Scrape With Headless Firefox
Discover how to use headless Firefox with Selenium, Playwright, and Puppeteer for web scraping, including practical examples for each library.
When web scraping, we often want to retrieve full page source (full HTML of the web page) we can parse it for data using tools like Cheerio. Using Puppeteer, to get the page source we can use page.content()
method:
const puppeteer = require('puppeteer');
async function run() {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto("https://httpbin.dev/html");
let source = await page.content();
// OR the faster method that doesn't wait for images to load:
let source = await page.content({"waitUntil": "domcontentloaded"});
console.log(source);
browser.close();
}
run();
⚠ It's possible that this command will retrieve page source before the page fully loads if it's a dynamic javascript page. For more see How to wait for a page to load in Puppeteer?