Finding Cheerio Elements Without a Specific Attribute
When a page mixes styled and unstyled elements, selecting by a present attribute can miss the ones you actually need.
Cheerio supports a CSS pattern for the opposite case: pick a tag, then exclude elements that carry a given attribute. That makes it straightforward to isolate plain div elements, links without an href, or other markup that lacks a marker attribute.
The selector pattern
Cheerio accepts familiar CSS selectors, so the exclusion can be expressed directly in the selector string. The :not() pseudo-class negates what follows it, and an attribute selector such as [class] tests for the presence of that attribute. Putting the two together, div:not([class]), means: match div elements, but only those for which a class attribute is absent. The same construction works for any attribute name you need to exclude, such as id, href, or data attributes, as long as the attribute is named inside the brackets.
Worked example
The protected example module code0001 loads a small HTML fragment containing three div elements: two carry a class attribute and one does not. After loading the fragment with cheerio.load, the selector div:not([class]) is applied, and each match is visited with .each() to print its text. Only the middle div, the one without a class, is printed. That result confirms the distinction between presence and absence: elements with class="content" or class="footer" are excluded, while the plain div remains.
A quick verification checklist
Before running the selector, check three things. First, confirm the tag name matches the markup you are targeting, since the exclusion only applies within that tag. Second, confirm the attribute name is spelled exactly as it appears in the HTML, because [class] and a differently named attribute test for different things. Third, inspect the output against the original markup: every returned element should lack the named attribute, and every element that has it should be absent from the results. If results look wrong, the most common cause is a mismatch between the tag or attribute name in the selector and the actual page structure.
Related selection tasks
Excluding by attribute absence is one part of a larger selection workflow. Related tasks include finding elements by a present attribute, finding elements by class, selecting across multiple tags, and reading titles or links. Those companion guides build on the same selector approach, so moving between presence and absence tests is a matter of adjusting the selector rather than learning a new API.
Frequently asked questions
How do I select a tag without a particular attribute in Cheerio?
Combine the tag you want with :not() around an attribute-presence selector. For example, div:not([class]) selects div elements that do not carry a class attribute, while div:not([id]) would select div elements without an id.
Can I use the same pattern for attributes other than class?
The attribute name inside the brackets is the part you change. Keep the same tag and :not() structure, then replace the attribute name with the one you want to exclude.