Last active
February 6, 2019 17:56
-
-
Save Raidus/9a956402141e9e38ce07d321d77285bb to your computer and use it in GitHub Desktop.
Load cheerio parser in Puppeteer/Chrome
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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