Automation/app/Libraries/CrawlerLibrary.php
2024-09-02 00:04:12 +09:00

46 lines
1.2 KiB
PHP

<?php
namespace App\Libraries;
use Symfony\Component\DomCrawler\Crawler;
class CrawlerLibrary extends WebBaseLibrary
{
public function __construct(string $host)
{
parent::__construct($host);
}
final public function getContent(string $url): string
{
$response = $this->getClient()->request('GET', $this->gethost() . $url);
return $response->getBody()->getContents();
}
final public function getInnerHTML(string $url, $tag = false)
{
$crawler = new Crawler($this->getContent($url));
return $tag ? $crawler->filter($tag)->html() : $crawler->html();
}
final public function getLinks(string $html, string $tag = "a"): array
{
$crawler = new Crawler($html);
return $crawler->filter($tag)->each(
function (Crawler $node) {
return $node->attr("href");
}
);
}
final public function getImages(string $html, $tag = "img"): array
{
$crawler = new Crawler($html);
return $crawler->filter($tag)->each(
function (Crawler $node) {
return $node->attr("src");
}
);
}
}