DOM Fetching and Querying

node v10.24.1
version: 1.0.0
endpointsharetweet
At some point, you'll need to inspect or scrape web pages for information. Having great libraries makes this process far simpler, so let's see how simple it is to fetch the DOM from a URL using got:
const got = require("got") const baseUrl = "https://darrenhickling.com" const body = (await got(baseUrl)).body
... then query it to find a simple element with cheerio:
const cheerio = require("cheerio") const $ = cheerio.load(body) const firstImage = baseUrl + $("img").attr("src")
... and finally, let's extract just the information we need from multiple elements:
const postTitles = $(".post-link") .map((_, el) => $(el).text()) .get() .map(text => text.trim())
Now I can easily double-check that the titles on my home page are what I expected, by hitting the endpoint of this RunKit. In conjunction with Cypress.io, of course 🧪
module.exports.endpoint = (_, res) => res.end(JSON.stringify(postTitles))
Loading…

no comments

    sign in to comment