Sangu scraper boiler plate
This commit is contained in:
23
app/Services/Feeds/SanguFeed.php
Normal file
23
app/Services/Feeds/SanguFeed.php
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
<?php
|
||||||
|
namespace App\Services\Feeds;
|
||||||
|
|
||||||
|
use Goutte\Client;
|
||||||
|
|
||||||
|
class SanguFeed
|
||||||
|
{
|
||||||
|
protected $client;
|
||||||
|
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
$this->client = new Client();
|
||||||
|
}
|
||||||
|
public function get()
|
||||||
|
{
|
||||||
|
|
||||||
|
$crawler = $this->client->request('GET', "http://sangu.mv/");
|
||||||
|
|
||||||
|
return $crawler->filter('div[class*="large-5 medium-4 large-pull-7 medium-pull-8 small-12 columns"] .news-list .list-title a')
|
||||||
|
->extract(['_text', '_attr' => 'href']);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
57
app/Services/Scrapers/SanguScraper.php
Normal file
57
app/Services/Scrapers/SanguScraper.php
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Services\Scrapers;
|
||||||
|
|
||||||
|
use Goutte\Client;
|
||||||
|
use Illuminate\Support\Carbon;
|
||||||
|
|
||||||
|
class SanguScraper
|
||||||
|
{
|
||||||
|
protected $client;
|
||||||
|
|
||||||
|
|
||||||
|
protected $content;
|
||||||
|
protected $topics = [];
|
||||||
|
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
$this->client = new Client;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function extract($url)
|
||||||
|
{
|
||||||
|
|
||||||
|
$crawler = $this->client->request('GET', $url);
|
||||||
|
|
||||||
|
$title = $crawler->filter('.brk-title2')->first()->text();
|
||||||
|
$image = $crawler->filter('.main-img img')->first()->attr('src');
|
||||||
|
$author = $crawler->filter('.author span')->first()->text();
|
||||||
|
|
||||||
|
$crawler->filter('article p')->each(function ($node) {
|
||||||
|
$this->content[] = preg_replace("/[a-zA-Z]/","",$node->text());
|
||||||
|
});
|
||||||
|
|
||||||
|
$crawler->filter('.article-tags')->each(function ($node) {
|
||||||
|
|
||||||
|
$this->topics[] = [
|
||||||
|
"name" => $node->text(),
|
||||||
|
"slug" => str_replace("https://mihaaru.com/", "", $node->attr('href'))
|
||||||
|
];
|
||||||
|
});
|
||||||
|
|
||||||
|
//Remove all the alphabets from string
|
||||||
|
//preg_replace("/[a-zA-Z]/", "",$string);
|
||||||
|
return [
|
||||||
|
'source' => 'Sangu',
|
||||||
|
'title' => $title,
|
||||||
|
'og_title' => $crawler->filter('meta[property*="og:title"]')->first()->attr('content'),
|
||||||
|
'image' => $image,
|
||||||
|
'content' => $this->content,
|
||||||
|
'url' => $url,
|
||||||
|
'date' => "",
|
||||||
|
'guid' => str_replace("https://mihaaru.com/news/","",$url),
|
||||||
|
'author' => $author,
|
||||||
|
'topics' => $this->topics
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user