FriendsOfPHP / Goutte

Goutte, a simple PHP Web Scraper
MIT License
9.26k stars 1.01k forks source link

count h1 tags #323

Open Takuaraa opened 6 years ago

Takuaraa commented 6 years ago

Hello,

Is there a way to count how many h1 tags a given url has? How can I increment $count using this: ?

$client = new Client();

    $crawler = $client->request('GET', $url);

    $count = 0;

    $crawler->filter('h1')->each(function ($node) {
      $count++;
      print $count++ . "<br>";
    });

This prints always 1 even if there are more h1 tags.

Thank you.

ssanders commented 6 years ago

If you just need the total count…

$h1 = $crawler->filter('h1')->each(function ($node) {
  return $node->text();
});

print count($h1);

If you want to print each increment…

$crawler->filter('h1')->each(function ($node) use ($count) {

Or…

$crawler->filter('h1')->each(function ($node) {
  global $count;
dpde commented 6 years ago

Why not:

$crawler->filter('h1')->count();
vinodsin commented 5 years ago

Why not:

$crawler->filter('h1')->count();

Best Solution