From 7d4815fcfafe4fdbd8e54cc8ce5fa2469bbfc2f9 Mon Sep 17 00:00:00 2001 From: Mohamed Jinas Date: Sat, 15 Aug 2020 02:24:57 +0500 Subject: [PATCH] Sangu scraper boiler plate --- app/Services/Feeds/SanguFeed.php | 23 +++++++++++ app/Services/Scrapers/SanguScraper.php | 57 ++++++++++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 app/Services/Feeds/SanguFeed.php create mode 100644 app/Services/Scrapers/SanguScraper.php diff --git a/app/Services/Feeds/SanguFeed.php b/app/Services/Feeds/SanguFeed.php new file mode 100644 index 0000000..effbc44 --- /dev/null +++ b/app/Services/Feeds/SanguFeed.php @@ -0,0 +1,23 @@ +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']); + + } +} \ No newline at end of file diff --git a/app/Services/Scrapers/SanguScraper.php b/app/Services/Scrapers/SanguScraper.php new file mode 100644 index 0000000..2250323 --- /dev/null +++ b/app/Services/Scrapers/SanguScraper.php @@ -0,0 +1,57 @@ +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 + ]; + } +}