simple-html-domvscheerio
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.
cheerio is a popular JavaScript library that allows you to interact with and manipulate HTML and XML documents in a similar way to how you would with jQuery in a browser. It is a fast, flexible, and lean implementation of core jQuery designed specifically for the server.
One of the main benefits of using cheerio is that it allows you to use jQuery-like syntax to navigate and m anipulate the Document Object Model (DOM) of an HTML or XML document, making it easy to work with.
cheerio supports CSS selectors though not XPath.
Highlights
Example Use
<?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();
const cheerio = require('cheerio');
const $ = cheerio.load('<html><head><title>My title</title></head><body><h1 class='name'>Hello World!</h1></body></html>');
// use css selectors
console.log($('title').text()); // My title
console.log($('.name').text()); // Hello World!
// select multiple elements
const $ = cheerio.load('<html><body><ul><li>item 1</li><li>item 2</li></ul></body></html>');
$('li').each(function(i, elem) {
console.log($(this).text());
});
// modify elements
const $ = cheerio.load('<html><body><h1>Hello World!</h1></body></html>');
$('h1').text('Hello, Cheerio!');
console.log($.html());