How to scrape tables with DOM Crawler?

You can scrape tables with
DOM Crawler
by combining the regular
CSS selectors
with the
filter
and each methods to iterate over the rows and cells of the table.

Here is some sample code that demonstrates how to scrape a simple HTML table using DOM Crawler:

use SymfonyComponentDomCrawlerCrawler;

$html = <<<EOD
                                                                                  
Name Age Occupation
Yasoob 35 Software Engineer
Pierre 28 Product Manager
EOD; // Load the HTML document $crawler = new Crawler($html); // Find the table element $table = $crawler->filter('table')->first(); // Loop over the rows of the table $table->filter('tr')->each(function ($row, $i) { // Loop over the columns of the row $row->filter('td')->each(function ($column, $j) { // Print the text content of the column echo $column->text() . PHP_EOL; }); }); // Output: // Yasoob // 35 // Software Engineer // Pierre // 28 // Product Manager

Related DOM Crawler web scraping questions:

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *