-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscraping-a-price-list.php
91 lines (59 loc) · 1.64 KB
/
scraping-a-price-list.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php
require 'vendor/autoload.php';
use GuzzleHttp\Client;
use Symfony\Component\DomCrawler\Crawler;
$products = [];
$discounted_products = [];
$best_price_products = [];
$client = new Client();
$response = $client->request('GET', 'http://testing-ground.scraping.pro/blocks', [
'allow_redirects' => true
]);
$body = $response->getBody()->getContents();
$crawler = new Crawler($body);
// CASE 1: get product names
$filter = $crawler->filter('div#case1 > div > span[style="float: left"] > .name');
if(count($filter) > 0)
{
foreach ($filter as $i => $content)
{
$products[$i]['name'] = $content->nodeValue;
}
}
// CASE 1: get product descriptions
$filter = $crawler->filter('div#case1 > div > span[style="float: left"]');
if(count($filter) > 0)
{
foreach ($filter as $i => $content)
{
$desc = str_replace($products[$i]['name'], "", $content->nodeValue);
$products[$i]['desc'] = $desc;
}
}
// CASE 1: get product prices
$filter = $crawler->filter('div#case1 > div > span[style="float: right"]');
if(count($filter) > 0)
{
foreach ($filter as $i => $content)
{
if(strpos($content->nodeValue, "discount") !== false)
{
$price = explode("discount", $content->nodeValue);
$products[$i]['has_discount'] = true;
$products[$i]['price'] = $price[0];
// CASE 1: get discounted price products
$discounted_products[] = $products[$i];
}
else{
$products[$i]['price'] = $content->nodeValue;
}
// CASE 1: get best price products
if($content->getAttribute('class') == 'best')
{
$best_price_products[] = $products[$i];
}
}
}
print_r($products);
print_r($discounted_products);
print_r($best_price_products);