Sangu scraper boiler plate
This commit is contained in:
		
							
								
								
									
										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