Skip to content

cssselectvssimple-html-dom

NOASSERTION 12 11 314
23.5 million (month) Apr 14 2012 1.6.0(2026-10-09 20:05:08 ago)
- - - MIT
Nov 09 2019 2.0 thousand (month) 2.0-RC2(2019-11-09 15:42:50 ago)

cssselect is a BSD-licensed Python library to parse CSS3 selectors and translate them to XPath 1.0 expressions.

XPath 1.0 expressions can be used in lxml or another XPath engine to find the matching elements in an XML or HTML document.

cssselect is used by other popular Python packages like parsel and scrapy but can also be used on it's own to generate valid XPath 1.0 expressions for parsing HTML and XML documents in other tools.

Note that because XPath selectors are more powerful than CSS selectors this translation is only possible one way. Converting XPath to CSS selectors is impractical and not supported by cssselect.

Simple HTML DOM Parser is a lightweight PHP library for parsing and manipulating HTML documents using a jQuery-like syntax. It is one of the most widely used HTML parsers in the PHP ecosystem, known for its simplicity and low learning curve.

Key features include:

  • jQuery-like selectors Find elements using CSS selectors similar to jQuery: find('#id'), find('.class'), find('div > p'), etc.
  • Easy element access Access element attributes, inner text, and inner HTML with simple property access: $element->plaintext, $element->href, $element->innertext.
  • DOM manipulation Modify element attributes, text content, and HTML. Add or remove elements from the document tree.
  • File and string parsing Parse HTML from strings, local files, or URLs directly.
  • Nested element traversal Navigate parent, child, and sibling elements with built-in traversal methods.
  • Memory-friendly Designed to handle large HTML documents efficiently with explicit memory cleanup.

Simple HTML DOM is best suited for quick scraping tasks and small projects where simplicity is more important than performance. For large-scale or performance-critical applications, consider using PHP's built-in DOMDocument or Symfony's DomCrawler which offer better performance and standards compliance.

Highlights


css-selectorsdsl-selectors

Example Use


from cssselect import GenericTranslator, SelectorError

translator = GenericTranslator()
try:
    expression = translator.css_to_xpath('div.content')
    print(expression)
    'descendant-or-self::div[@class and contains(concat(' ', normalize-space(@class), ' '), ' content ')]'
except SelectorError as e:
    print(f'Invalid selector {e}')
<?php
require 'vendor/autoload.php';

use simplehtmldom\HtmlWeb;
use simplehtmldom\HtmlDocument;

// Parse HTML from a URL
$web = new HtmlWeb();
$html = $web->load('https://example.com');

// Or parse from a string
$html = new HtmlDocument('<div><p class="intro">Hello World</p></div>');

// Find elements using CSS-like selectors
$title = $html->find('title', 0)->plaintext;
echo "Title: $title\n";

// Find all products
foreach ($html->find('.product') as $product) {
    $name = $product->find('.name', 0)->plaintext;
    $price = $product->find('.price', 0)->plaintext;
    $link = $product->find('a', 0)->href;
    echo "$name: $price ($link)\n";
}

// Modify elements
$html->find('h1', 0)->innertext = 'Modified Title';

// Clean up memory when done
$html->clear();

Alternatives / Similar


Was this page helpful?