Advertisement
moonelite99

Crawl Tikibook

Dec 29th, 2021
1,353
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.74 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Scraper;
  4.  
  5. use App\Models\Book;
  6. use App\Models\Shopeebook;
  7. use App\Models\Tikibook;
  8. use Exception;
  9. use InvalidArgumentException;
  10. use Symfony\Component\DomCrawler\Crawler;
  11.  
  12. class BookScraper
  13. {
  14.     public function scrapeTikiBook()
  15.     {
  16.         $base = 'https://tiki.vn/sach-truyen-tieng-viet/c316?page=';
  17.  
  18.         for ($i = 1; $i <= 21; $i++) {
  19.             $url = $base . $i;
  20.  
  21.             $data = file_get_contents($url);
  22.             $crawler = new Crawler($data);
  23.  
  24.             $crawler->filter('div.product-box-list div.product-item')->each(
  25.                 function (Crawler $node) {
  26.                     $link = $node->filter('a')->attr('href');
  27.                     if (strpos($link, '//') === false) {
  28.  
  29.                         $title = $node->filter('a')->attr('title');
  30.                         if (strpos($title, "Combo") !== false) {
  31.                         } else {
  32.                             if (strpos($title, "(") !== false) {
  33.                                 $title = substr($title, 0, strpos($title, "("));
  34.                             }
  35.                             if (strpos($title, "Tặng") !== false) {
  36.                                 $title = substr($title, 0, strpos($title, "Tặng"));
  37.                             }
  38.                             $link = 'https://tiki.vn' . $link;
  39.                             $book_id = $node->filter('a')->attr('data-id');
  40.  
  41.                             Tikibook::updateOrCreate([
  42.                                 'title' => $title,
  43.                                 'link' => $link,
  44.                                 'book_id' => $book_id,
  45.                             ]);
  46.                         }
  47.                     }
  48.                 }
  49.             );
  50.         }
  51.     }
  52.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement