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 Symfony\Component\DomCrawler\Crawler;

$html = <<<EOD
      <td>Software Engineer</td>
      <td>Product Manager</td>

// 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: