Skip to content

Extracting Links With Cheerio in Node.js

To pull URLs from a page, first narrow the DOM to the elements you care about, then read the address stored on each one.

OUTPUT FORMATSHTMLJSONMarkdown

In Cheerio that means selecting anchor elements and reading their href attribute for every match.

The two-step pattern: select, then read href

Start with a CSS-style selector that matches only the anchors you want, for example all a elements on the page. Cheerio returns that set as a collection, so you can iterate it once instead of querying repeatedly.

For each element in the collection, two pieces of information are usually useful together: the visible label and the destination. Reading the element's text gives you the label, while reading its href attribute gives you the URL. Keeping them paired in the same iteration prevents mismatches when you later sort, filter, or deduplicate results.

A practical check before you trust the output: confirm that every collected item came from an anchor, that the href was read from that same element, and that empty or missing href values are handled separately rather than silently included as blank links.

What the example does, step by step

The complete Node.js example is supplied as protected module code0001. Its workflow is: fetch a page as text, load that HTML into Cheerio, select all anchor tags, loop over them, and print each element's text alongside its href. A failed fetch is caught and reported as an error message instead of continuing with empty HTML.

Use that structure as a template and adapt only the parts that belong to your task: the URL you fetch, the selector if you need a subset of links, and what you do with each text and href pair after printing it.

Deciding what to keep

If you only need link destinations, collect the href values in order. If you need a usable link list for navigation or further crawling, keep the text and href together so a human can tell what each URL represents.

If a page contains many anchors, a broad a selector will return all of them, including navigation, footers, and in-page anchors. Narrow the selector to the container or class that holds the links you actually want when that happens, rather than filtering a long list afterwards.