Automation/app/Traits/MyCrawlerTrait.php
2024-09-04 18:59:45 +09:00

24 lines
704 B
PHP

<?php
namespace App\Traits;
use Symfony\Component\DomCrawler\Crawler;
trait MyCrawlerTrait
{
final protected function createByMyCrawler($html)
{
return new Crawler($html);
}
public function getTagDatasByMyCrawler(Crawler $crawler, array $options = ["tag" => "a", "attr" => "href"], array $tagdatas = []): array
{
$crawler->filter($options["tag"])->each(
function (Crawler $node) use (&$tagdatas, &$options): void {
log_message("debug", sprintf("getTagDatas-> %s:%s", $options["tag"], $node->attr($options["attr"])));
$tagdatas[] = $node->attr($options["attr"]);
}
);
return $tagdatas;
}
}