|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace AmineBenHariz\TunisianetScraper; |
|
4
|
|
|
|
|
5
|
|
|
use AmineBenHariz\TunisianetScraper\Entity\Category; |
|
6
|
|
|
use AmineBenHariz\TunisianetScraper\Entity\Product; |
|
7
|
|
|
use Goutte\Client; |
|
8
|
|
|
use Symfony\Component\DomCrawler\Crawler; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class Scraper |
|
12
|
|
|
* @package AmineBenHariz\TunisianetScraper |
|
13
|
|
|
*/ |
|
14
|
|
|
class Scraper |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var Client |
|
18
|
|
|
*/ |
|
19
|
|
|
private $client; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Scraper constructor. |
|
23
|
|
|
*/ |
|
24
|
|
|
public function __construct() |
|
25
|
|
|
{ |
|
26
|
|
|
$this->client = new Client(); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @param int $productId |
|
31
|
|
|
* @return Product |
|
32
|
|
|
*/ |
|
33
|
|
|
public function getProductById($productId) |
|
34
|
|
|
{ |
|
35
|
|
|
$url = 'http://www.tunisianet.com.tn/x/'.$productId.'-x.html'; |
|
36
|
|
|
|
|
37
|
|
|
$crawler = $this->client->request('GET', $url); |
|
38
|
|
|
|
|
39
|
|
|
$url = $this->client->getHistory()->current()->getUri(); |
|
40
|
|
|
|
|
41
|
|
|
$title = $crawler->filter('h1 > acronym')->text(); |
|
42
|
|
|
$reference = $crawler->filter('#hit_ref')->attr('value'); |
|
43
|
|
|
$priceTag = $crawler->filter('#our_price_display')->text(); |
|
44
|
|
|
$price = floatval(str_replace(',', '.', $priceTag)); |
|
45
|
|
|
$description = $crawler->filter('#short_description_content')->text(); |
|
46
|
|
|
$available = $crawler->filter('form#buy_block > div')->attr('class') === 'en_stock'; |
|
47
|
|
|
|
|
48
|
|
|
$product = new Product($productId, $title, $reference, $description, $price, $available, $url); |
|
49
|
|
|
|
|
50
|
|
|
return $product; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param int $categoryId |
|
55
|
|
|
* @return Category |
|
56
|
|
|
*/ |
|
57
|
|
|
public function getCategoryById($categoryId) |
|
58
|
|
|
{ |
|
59
|
|
|
$url = 'http://www.tunisianet.com.tn/'.$categoryId.'-'; |
|
60
|
|
|
$crawler = $this->client->request('GET', $url); |
|
61
|
|
|
|
|
62
|
|
|
$url = $this->client->getHistory()->current()->getUri(); |
|
63
|
|
|
|
|
64
|
|
|
$title = $crawler->filter('title')->text(); |
|
65
|
|
|
|
|
66
|
|
|
|
|
67
|
|
|
$products = $crawler->filter('#produit_liste_main .ajax_block_product')->each(function (Crawler $node) { |
|
68
|
|
|
$title = $node->filter('.center_block > h2 > a')->text(); |
|
69
|
|
|
$id = intval($node->filter("input[id^='hit_id']")->attr('value')); |
|
70
|
|
|
$reference = $node->filter("input[id^='hit_ref']")->attr('value'); |
|
71
|
|
|
$description = $node->filter('.center_block > .product_desc > a')->text(); |
|
72
|
|
|
$priceTag = $node->filter('.price')->text(); |
|
73
|
|
|
$price = floatval(str_replace(',', '.', $priceTag)); |
|
74
|
|
|
$available = $node->filter('#produit_liste_prix > div:nth-child(2)')->attr('class') === 'en_stock'; |
|
75
|
|
|
$url = $node->filter('.center_block > h2 > a')->attr('href'); |
|
76
|
|
|
|
|
77
|
|
|
$product = new Product($id, $title, $reference, $description, $price, $available, $url); |
|
78
|
|
|
|
|
79
|
|
|
return $product; |
|
80
|
|
|
}); |
|
81
|
|
|
|
|
82
|
|
|
$category = new Category($categoryId, $title, $url, $products); |
|
83
|
|
|
return $category; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|