Skip to main content

Best practices

  • Use specific and unique class names to ensure accurate selection and avoid confusion with other elements.

  • When targeting multiple classes, ensure they are correctly concatenated without spaces in the selector string to match elements with all specified classes.

  • Regularly update and test your selectors if the source website changes its layout or class naming conventions to maintain the accuracy of your data extraction.

  • Utilize Cheerio's built-in functions like .text() or .html() to directly extract and manipulate the content of selected elements based on your requirements.

Scrollable code block. Use arrow keys to scroll.

Common issues

  • Ensure that the class names used in your selectors are case-sensitive and match exactly with those in the HTML to avoid missing elements.

  • Debug issues where no elements are returned by logging the entire fetched HTML to verify that the page has fully loaded and contains the expected classes.

  • Avoid using overly broad class names that might return more elements than intended, which can lead to performance issues or incorrect data scraping.

  • When using Cheerio, remember that it operates on a static snapshot of the HTML, so dynamic changes made by JavaScript after page load won't be reflected.

Scrollable code block. Use arrow keys to scroll.

Try Oxylabs' Proxies & Web API

Residential Proxies

175M+ ethically sourced IPs for top-tier reliability

From

15

Datacenter Proxies

Fast cost-efficient infrastructure for any workload

From

15

Web API

One API for web discovery and data extraction

From

15

Useful resources

Web Scraping With Java
Maryia Stsiopkina avatar

Maryia Stsiopkina

2026-04-03

How to Find Elements With Selenium in Python
How to Find Elements With Selenium in Python
Enrika avatar

Enrika Pavlovskytė

2024-06-21

XPath vs CSS Selectors
Monika Maslauskaite avatar

Monika Maslauskaite

2021-07-13

Get the latest news from data gathering world

Scale up your business with Oxylabs