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

56 lines
2.1 KiB
PHP

<?php
namespace App\Libraries\MyCrawler;
use Symfony\Component\DomCrawler\Crawler;
class YamapLibrary extends MyCrawlerLibrary
{
public function __construct(string $host)
{
parent::__construct($host);
}
public function getLinks($html, array $options = ["skip" => "관리자"]): array
{
//div.bbs_item를 가진 객체를 찾아서 배열로 넘김
$domElements = $this->getCrawler($html)->filter("div.bbs_list div.bbs_item")->first()->siblings();
$links = [];
foreach ($domElements as $domElement) {
$this->getCrawler($domElement)->filter("span.g_nickname")->each(function (Crawler $node) use (&$options, &$links, &$domElement) {
if ($node->text() != $options["skip"]) {
$links[] = ["anchor" => $node->text(), "href" => $this->getCrawler($domElement)->filter("a.list_subject")->attr("href")];
}
});
}
return $links;
}
public function getImages(string $html, array $options = ["tag" => "img", "attr" => "src"]): array
{
//div.contents 가진 객체를 찾아서 첫번쨰 요소에서만 참조
$domElement = $this->getCrawler($html)->filter("div.contents")->first();
return $domElement->filter($options["tag"])->each(
function (Crawler $node) use (&$options) {
return [
"alt" => $node->attr('alt'),
"src" => $node->attr($options["attr"])
];
}
);
}
public function getVideos(string $html, array $options = ["tag" => "video", "attr" => "src"]): array
{
//div.contents 가진 객체를 찾아서 첫번쨰 요소에서만 참조
$domElement = $this->getCrawler($html)->filter("div.contents")->first();
return $domElement->filter($options["tag"])->each(
function (Crawler $node) use (&$options) {
return [
"alt" => $node->attr('alt'),
"src" => $node->attr($options["attr"])
];
}
);
}
}