Best practices

  • Use JavaScript and a library like jQuery or Cheerio for text selection in HTML elements by utilizing the :contains() method.

  • When using text-based selectors, consider the impact of dynamic content changes and ensure your selector method is updated accordingly.

  • For performance optimization, avoid overly broad text selectors in large documents; instead, target more specific elements or use class and ID selectors to narrow down the selection.

  • Remember to handle cases where the text may change due to localization or updates, possibly using data attributes or other stable identifiers instead of text content.

// Install dependencies:
// npm install jsdom jquery

const jsdom = require('jsdom');
const { JSDOM } = jsdom;
const jquery = require('jquery');

// Using built-in fetch available in Node.js v18+
fetch('https://sandbox.oxylabs.io/products')
  .then(res => res.text())
  .then(html => {
    const dom = new JSDOM(html);
    const $ = jquery(dom.window);
    
    // Use jQuery to select elements by text
    $('h4:contains("Zelda")').each(function() {
      console.log($(this).text().trim());
    });
  });

Try Oyxlabs' Proxies & Scraper API

Residential Proxies

Self-Service

Human-like scraping without IP blocking

From

8

Datacenter Proxies

Self-Service

Fast and reliable proxies for cost-efficient scraping

From

1.2

Web scraper API

Self-Service

Public data delivery from a majority of websites

From

49

Other CSS web scraping questions

Useful resources

Get the latest news from data gathering world

I'm interested