Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\Scraper;
- use App\Models\Book;
- use App\Models\Shopeebook;
- use App\Models\Tikibook;
- use Exception;
- use InvalidArgumentException;
- use Symfony\Component\DomCrawler\Crawler;
- class BookScraper
- {
- public function scrapeTikiBook()
- {
- $base = 'https://tiki.vn/sach-truyen-tieng-viet/c316?page=';
- for ($i = 1; $i <= 21; $i++) {
- $url = $base . $i;
- $data = file_get_contents($url);
- $crawler = new Crawler($data);
- $crawler->filter('div.product-box-list div.product-item')->each(
- function (Crawler $node) {
- $link = $node->filter('a')->attr('href');
- if (strpos($link, '//') === false) {
- $title = $node->filter('a')->attr('title');
- if (strpos($title, "Combo") !== false) {
- } else {
- if (strpos($title, "(") !== false) {
- $title = substr($title, 0, strpos($title, "("));
- }
- if (strpos($title, "Tặng") !== false) {
- $title = substr($title, 0, strpos($title, "Tặng"));
- }
- $link = 'https://tiki.vn' . $link;
- $book_id = $node->filter('a')->attr('data-id');
- Tikibook::updateOrCreate([
- 'title' => $title,
- 'link' => $link,
- 'book_id' => $book_id,
- ]);
- }
- }
- }
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement