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
  <table>
    <tr>
      <th>Name</th>
      <th>Age</th>
      <th>Occupation</th>
    </tr>
    <tr>
      <td>Yasoob</td>
      <td>35</td>
      <td>Software Engineer</td>
    </tr>
    <tr>
      <td>Pierre</td>
      <td>28</td>
      <td>Product Manager</td>
    </tr>
  </table>
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: