Files
karudhaas/app/Services/Scrapers/ThePressScraper.php
2020-08-14 21:57:16 +05:00

62 lines
1.7 KiB
PHP

<?php
namespace App\Services\Scrapers;
use Goutte\Client;
class ThePressScraper
{
protected $client;
protected $content;
protected $author;
protected $topics = [];
public function __construct()
{
$this->client = new Client;
}
public function extract($url)
{
$crawler = $this->client->request('GET', $url);
$title = $crawler->filter('h1')->first()->text();
$image = $crawler->filter('.article-image img')->first()->attr('src');
$crawler->filter('article p')->each(function ($node) {
$this->content[] = preg_replace("/[a-zA-Z]/","",$node->text());
});
$crawler->filter('.article-tags a')->each(function ($node) {
$this->topics[] = [
"name" => $node->text(),
"slug" => str_replace("https://thepress.mv/", "", $node->attr('href'))
];
});
if($crawler->filter(".author-details strong")->count() == 1)
{
$this->author = $crawler->filter('.author-details strong')->first()->text();
}
//Remove all the alphabets from string
//preg_replace("/[a-zA-Z]/", "",$string);
return [
'source' => 'The Press',
'title' => $title,
'og_title' => $crawler->filter('meta[property*="og:title"]')->first()->attr('content'),
'image' => $image,
'content' => $this->content,
'url' => $url,
'date' => $crawler->filter('.article-header .datetime')->first()->text(),
'guid' => str_replace("https://thepress.mv/","",$url),
'author' => $this->author,
'topics' => $this->topics
];
}
}