Skip to content

Instantly share code, notes, and snippets.

@Raidus
Last active February 6, 2019 17:56
Show Gist options
  • Save Raidus/9a956402141e9e38ce07d321d77285bb to your computer and use it in GitHub Desktop.
Save Raidus/9a956402141e9e38ce07d321d77285bb to your computer and use it in GitHub Desktop.
Load cheerio parser in Puppeteer/Chrome
// Inspired by: https://docs.k6.io/docs/modules#section-npm-modules
// 1. Browserify cheerio module
/*
git clone [email protected]:cheeriojs/cheerio.git
cd cheerio
npm install
browserify index.js -s cheerio > cheerio.js
*/
// 2. Load into Chrome
const inject_cheerio = fs.readFileSync("../lib/cheerio.js", "utf-8"); // see line 8
await page.evaluateOnNewDocument(inject_cheerio); // must be before page.goto(...)
// 3. Use e.g. in page.evaluate
let parsedHTML = await page.evaluate(() => {
const $ = cheerio.load(document.body.innerHTML);
const parsedField = $("#myDiv").text();
return parsedField;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment