Automation/app/Libraries/MyCrawler/MyCrawlerLibrary.php
2024-09-02 19:08:15 +09:00

43 lines
1.2 KiB
PHP

<?php
namespace App\Libraries\MyCrawler;
use App\Libraries\MyBaseLibrary;
use Symfony\Component\DomCrawler\Crawler;
abstract class MyCrawlerLibrary extends MyBaseLibrary
{
protected function __construct(string $host)
{
parent::__construct($host);
}
final public function getCrawler($html)
{
return new Crawler($html);
}
final public function getInnerHTML(string $html, $tag = false)
{
return $tag ? $this->getCrawler($html)->filter($tag)->html() : $this->getCrawler($html)->html();
}
public function getLinks(string $html, array $options = ["tag" => "a", "attr" => "href"]): array
{
return $this->getCrawler($html)->filter($options["tag"])->each(
function (Crawler $node) use (&$options) {
return ["anchor" => $node->text(), "href" => $node->attr($options["attr"])];
}
);
}
public function getImages(string $html, array $options = ["tag" => "img", "attr" => "src"]): array
{
return $this->getCrawler($html)->filter($options["tag"])->each(
function (Crawler $node) use (&$options) {
return ["alt" => $node->text(), "src" => $node->attr($options["attr"])];
}
);
}
}