80 lines
2.2 KiB
PHP
80 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace App\Libraries\MyCrawler;
|
|
|
|
use Symfony\Component\DomCrawler\Crawler;
|
|
|
|
class MyCrawlerLibrary
|
|
{
|
|
private $_debug = false;
|
|
|
|
public function __construct() {}
|
|
|
|
final public function getDebug(): bool
|
|
{
|
|
return $this->_debug;
|
|
}
|
|
final public function setDebug(bool $debug): void
|
|
{
|
|
$this->_debug = $debug;
|
|
}
|
|
|
|
final public function createCrawler($html)
|
|
{
|
|
return new Crawler($html);
|
|
}
|
|
|
|
final public function getInnerHTML(string $html, $tag = false)
|
|
{
|
|
return $tag ? $this->createCrawler($html)->filter($tag)->html() : $this->createCrawler($html)->html();
|
|
}
|
|
|
|
public function getLinks(Crawler $crawler, array $options = ["tag" => "a", "attr" => "href"]): array
|
|
{
|
|
$links = $crawler->filter($options["tag"])->each(
|
|
function (Crawler $node) use (&$options): array {
|
|
return [
|
|
"anchor" => $node->text(),
|
|
"href" => $node->attr($options["attr"])
|
|
];
|
|
}
|
|
);
|
|
foreach ($links as $link) {
|
|
log_message("debug", "Link-> " . $link['href']);
|
|
}
|
|
return $links;
|
|
}
|
|
|
|
public function getImages(Crawler $crawler, array $options = ["tag" => "img", "attr" => "src"]): array
|
|
{
|
|
$images = $crawler->filter($options["tag"])->each(
|
|
function (Crawler $node) use (&$options): array {
|
|
return [
|
|
"alt" => $node->attr('alt'),
|
|
"src" => $node->attr($options["attr"])
|
|
];
|
|
}
|
|
);
|
|
foreach ($images as $image) {
|
|
log_message("debug", "Image-> " . $image['src']);
|
|
}
|
|
return $images;
|
|
}
|
|
|
|
public function getVideos(Crawler $crawler, array $options = ["tag" => "video", "attr" => "src"]): array
|
|
{
|
|
$videos = $crawler->filter($options["tag"])->each(
|
|
function (Crawler $node) use (&$options): array {
|
|
return [
|
|
"alt" => $node->attr('alt'),
|
|
"src" => $node->attr($options["attr"])
|
|
];
|
|
}
|
|
);
|
|
foreach ($videos as $video) {
|
|
log_message("debug", "Video-> " . $video['src']);
|
|
}
|
|
return $videos;
|
|
}
|
|
}
|