How to find HTML elements by class with DOM Crawler?
Discover how to identify HTML elements by class using DOM Crawler’s filter method, with an example of extracting text from specific classes.


To find HTML elements by their class using DOM Crawler, you first need to fetch the HTML of the webpage you're interested in, which can be done using the Guzzle HTTP client. Once you have the HTML, load it into the DOM Crawler. Then, utilize the 'filter' method provided by DOM Crawler, passing in the specific class name prefixed with a dot (for example, '.mb-33') as the parameter. This method isolates all elements that have the specified class name. You can then loop through these elements and access their content. This process allows for efficient and targeted extraction of webpage elements based on their class attribute.
Fetching the Webpage
Start by pulling in the page you want to analyze. We use Guzzle here, a handy tool for making web requests:
<?php
use GuzzleHttp\Client;
// Set up a Guzzle client instance
$client = new Client();
// Make a request to the website and grab the HTML content
$response = $client->get('https://serply.io/');
$html = (string) $response->getBody();
?>In this code, Guzzle acts as your web browser, fetching the HTML from the webpage you specify.
Sifting Through the HTML
Now, with the webpage's HTML in hand, it's time to sift through it with DOM Crawler:
<?php
use Symfony\Component\DomCrawler\Crawler;
// Load the HTML into DOM Crawler
$crawler = new Crawler($html);
?>DOM Crawler takes the massive jumble of HTML and makes it something you can navigate and manipulate programmatically.
Isolating Specific Elements
Next, we focus on finding the elements marked with the class "mb-33" using the filter method:
<?php
// Use the filter method to find elements with class "mb-33"
$h1Tag = $crawler->filter('.mb-33');
?>The filter method here is like telling DOM Crawler, "Hey, please find all the pieces of the puzzle that have this specific marking on them."
Extracting and Using the Data
Once you've isolated these elements, you can loop through them and do what you need with their content:
<?php
foreach ($h1Tag as $element) {
// Output the text content of each element
echo $element->textContent . PHP_EOL;
}
?>This part of the code walks through each element you found and prints out its text, allowing you to see or use this information directly.
Wrapping It Up
To wrap things up, what we've done here is combine the power of Guzzle and DOM Crawler to zero in on specific parts of a webpage by class name. Using the 'filter' method alongside a CSS selector, we can quickly locate and work with elements like those labeled with "mb-33".
Through this method, you can efficiently sift through the vast information on a web page and extract just what you need, whether for data analysis, web scraping, or other purposes. It's a straightforward yet powerful approach to navigating the complex world of HTML content. For more comprehensive web scraping strategies and to manage search engine results more effectively, consider exploring tools like the Google SERP API and Google Crawl API, which can provide deeper insights and optimizations for your scraping tasks.