/
scraper.php
516 lines (451 loc) · 22.4 KB
/
scraper.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
<?php
require_once( "simple_html_dom.php" );
const BASE_URL = "http://www.amazon.co.uk/s/ref=nb_sb_noss?url=search-alias%3Daps&field-keywords=";
// We use the php similar_text method to match text within a certain percentage,
// as given here.
const MINIMUM_MATCH_LEVEL = 90;
const DEBUG_LEVEL = 4; // Print debug messages to console.
const ERROR_LEVEL_DEBUG = 4;
const ERROR_LEVEL_INFO = 3;
const ERROR_LEVEL_WARN = 2;
const ERROR_LEVEL_ERROR = 1;
const ERROR_LEVEL_FATAL = 0;
const ERROR_LEVELS = [ERROR_LEVEL_DEBUG => "DEBUG",
ERROR_LEVEL_INFO => "INFO",
ERROR_LEVEL_WARN => "WARNING",
ERROR_LEVEL_ERROR => "ERROR",
ERROR_LEVEL_FATAL => "FATAL ERROR"];
class Scraper
{
// This is the base URL for performing a search for a given product.
private $dataFile = null;
private $outputFile = null;
private $errorFile = null;
private $processedCount = 0;
private $successCount = 0;
private $errorCount = 0;
private $lineCount = 0;
public function __construct($dataFile, $outputFile, $errorFile)
{
// Check for and open our input and output files.
if( !file_exists( $dataFile ) )
$this->log( ERROR_LEVEL_FATAL, "Unable to open data file " . $dataFile . " - file does not exist." );
if( ( $this->dataFile = fopen( $dataFile, "r") ) == false )
$this->log( ERROR_LEVEL_FATAL, "Unable to open data file " . $dataFile );
if( ( $this->outputFile = fopen( $outputFile, "w" ) ) == false )
$this->log( ERROR_LEVEL_FATAL, "Unable to open " . $outputFile . " for writing." );
if( ( $this->errorFile = fopen( $errorFile, "w" ) ) == false )
$this->log( ERROR_LEVEL_FATAL, "Unable to open " . $errorFile . " for writing." );
// At this point, if anything has gone wrong, the process will have died,
// so we can continue.
}
public function scrape()
{
$searchTerms;
$listItems;
// Load up the source data file and start iterating through it.
while (($data = fgetcsv($this->dataFile, 1000, ",")) !== FALSE)
{
// We use a line counter so we can generate errors that refer to a
// specific line in the input file.
$this->lineCount ++;
// Ignore the first line as it has headers.
if( $this->lineCount == 1 ) continue;
// Keep track of the number of lines we've processed.
$this->processedCount ++;
if( count($data) < 5 ) // Not enough input fields, so reject the line.
{
$this->writeToErrorFile( $this->lineCount, (count($data) > 0 ? $data[0] : null), "Input data error - not enough fields." );
continue;
}
$SKU = trim( $data[0] );
$name = trim( $data[1] );
$description = trim( $data[2] );
$colour = trim( $data[3] );
$size = trim( $data[4] );
$this->log( ERROR_LEVEL_DEBUG, "Line " . $this->lineCount . "; Searching for \"" . $name . "\"" );
// Find a match based on the raw name. If there is a match, we get
// a link back. We always get a reference to the list returned,
// in case we need to look through the list for matching
// descriptions.
$listDOM = null;
$matchFound = false;
if( ( $link = $this->searchByName( $name, $listDOM ) ) != false )
{
$itemDOM = $this->getDOMFromURL( $link );
$this->scrapeASIN( $itemDOM, $SKU, $name, $description, $colour, $size, $this->lineCount );
} else
{
$this->log( ERROR_LEVEL_DEBUG, "Could not find a good name match. Trying without SKU / Colour prefix." );
if( $this->startsWithPartofWord( $name, $SKU ) )
{
// Trim the SKU off the start, which will be the
// first word, and search again.
$shortName = $this->deleteFirstWord( $name );
if( ( $link = $this->searchByName( $shortName, $listDOM ) ) != false )
{
$itemDOM = $this->getDOMFromURL( $link );
$this->scrapeASIN( $itemDOM, $SKU, $name, $description, $colour, $size, $this->lineCount );
$matchFound = true;
} else
{
// Searching without the SKU didn't work; let's try the
// same thing with the product colour.
if( $this->startsWithPartofWord( $shortName, $colour ) )
{
$shortName = $this->deleteFirstWord( $shortName );
if( ( $link = $this->searchByName( $shortName, $listDOM ) ) != false )
{
$itemDOM = $this->getDOMFromURL( $link );
$this->scrapeASIN( $itemDOM, $SKU, $name, $description, $colour, $size, $this->lineCount );
$matchFound = true;
}
}
}
} else
{
$this->log( ERROR_LEVEL_DEBUG, "Name does not start with SKU; falling back to description search." );
}
if( !$matchFound )
{
$this->log( ERROR_LEVEL_DEBUG, "Could not find a good enough match. Trying descriptions." );
// Iterate back through our matches, load up the product
// page and attempt to match based on description.
foreach( $listDOM->find( "li[class='s-result-item']" ) as $resultItem )
{
$item = $resultItem->find( "a[class='s-access-detail-page']", 0);
if( $item != null )
{
$link = $item->getAttribute("href");
$itemDOM = $this->getDOMFromURL( $link );
if( $this->matchDescription( $itemDOM, $SKU, $name, $description, $colour, $size, $this->lineCount ) )
{
$this->scrapeASIN( $itemDOM, $SKU, $name, $description, $colour, $size, $line );
$matchFound = true;
break;
}
}
}
if( !$matchFound )
$this->writeToErrorFile( $this->lineCount, $SKU, "Could not find good match." );
}
}
// Release all dom elements to defeat the memory leak.
$this->cleanAllDOM($GLOBALS);
} // while
// Finish off by closing the files.
if( $this->dataFile ) fclose( $this->dataFile );
if( $this->outputFile ) fclose( $this->outputFile );
if( $this->errorFile ) fclose( $this->errorFile );
$this->log( ERROR_LEVEL_INFO, "Processing complete." );
$this->log( ERROR_LEVEL_INFO, $this->processedCount . " lines processed." );
$this->log( ERROR_LEVEL_INFO, $this->successCount . " ASINs successfully found." );
$this->log( ERROR_LEVEL_INFO, $this->errorCount . " products not found. Check error file for details" );
}
private function searchByName( $name, &$listDOM )
{
$this->log( ERROR_LEVEL_DEBUG, "Line " . $this->lineCount . "; Searching for \"" . $name . "\"" );
// Grab the search page from Amazon based on search terms.
$listDOM = $this->loadSearch( $name );
// First look to see if nothing was found.
if( !is_null( $listDOM->getElementById("noResultsTitle") ) )
return false;
// We got some results, so let's iterate through them and get the
// % match. The highest match above our minimum is the one we take.
$listItems = array();
foreach( $listDOM->find( "li[class='s-result-item']" ) as $resultItem )
{
$item = $resultItem->find( "a[class='s-access-detail-page']", 0);
if( $item != null )
{
$title = htmlspecialchars_decode( $item->getAttribute("title") );
$link = $item->getAttribute("href");
similar_text( strtoupper( $name ), strtoupper( $title ), $matchLevel );
$listItems[] = array($matchLevel, $link, $title);
$this->log(ERROR_LEVEL_DEBUG, $matchLevel . "; \"" . $title . "\"" );
}
}
// Now we have an array full of possible matches, find the best
// match.
$bestMatch = null;
foreach( $listItems as $listItem )
{
if( is_null( $bestMatch ) )
{
$bestMatch = $listItem;
} else
{
if( $listItem[0] > $bestMatch[0] )
{
$bestMatch = $listItem;
}
}
}
// Now if the best match has a score lower than our maximum, we can
// declare success and return the link for the best match.
$this->log( ERROR_LEVEL_DEBUG, "Best match: " . $bestMatch[0] . "; " . $bestMatch[2] );
if( $bestMatch[0] >= MINIMUM_MATCH_LEVEL )
{
$this->log( ERROR_LEVEL_DEBUG, "Found a match with " . $bestMatch[0] . " " . $bestMatch[2] );
return $bestMatch[1];
}
return false;
}
private function matchDescription( $itemDOM, $SKU, $name, $description, $colour, $size, $line )
{
// We have a link to a product with an ambiguous name, so let's look
// for the description.
$this->log( ERROR_LEVEL_DEBUG, "Looking for description match: \"" . $description . "\"" );
// We look for the specific item description area in the page, otherwise
// we won't be able to do a fuzzy match as there's too much text to
// compare.
$descriptionElement = $itemDOM->find("div[id='productDescription']", 0);
if( !is_null( $descriptionElement ) )
{
$descriptionText = $this->cleanString( $descriptionElement->innertext );
$this->log( ERROR_LEVEL_DEBUG, "Found a description: \"" . $descriptionText . "\"" );
return $this->isLike( $description, $descriptionText );
} else
{
// No description, so let's look at product features.
$descriptionElement = $itemDOM->find("div[id='feature-bullets']", 0);
if( !is_null( $descriptionElement ) )
{
$descriptionText = $this->cleanString( $descriptionElement->innertext );
$this->log( ERROR_LEVEL_DEBUG, "Found some attributes: \"" . $descriptionText . "\"" );
return $this->isLike( $description, $descriptionText );
}
}
return false;
}
private function scrapeASIN( $itemDOM, $SKU, $name, $description, $colour, $size, $line )
{
$foundASIN = false;
// We have a link to the product that matches our search, so first load
// it up.
$this->log( ERROR_LEVEL_DEBUG, "Loading product page." );
// Now we need to load up the JSON object that has variations. If it's
// there, we can look for the colour and size variations given in our
// source file. If not, look on the main page to see if there's a match
// with our source data. If we can't find a match in either then we
// haven't found a valid product match and can't record the ASIN.
foreach( $itemDOM->find("script") as $javascriptElement )
{
$javascript = $javascriptElement->innertext;
if( preg_match( "/twister-js-init-mason-data/", $javascript ) )
{
// Now we have the correct javascript, parse it into a DOM object
// and look for our combinations. The next part of the code
// extracts text from within matching curly braces, which is
// where Amazon hides a JSON object containing the product type
// combinations we need.
preg_match_all('/{((?:[^{}]++|(?R))*+)}/', $javascript, $matches);
// We should have several entries as a result, one of which
// contains the JSON we need, so let's look for it.
foreach( $matches[1] as $possibleMatch )
{
if( preg_match( "/dataToReturn/", $possibleMatch ) )
{
// We have what we need, so grab the JSON.
preg_match_all('/{((?:[^{}]++|(?R))*+)}/', $possibleMatch, $jsonMatches);
$jsonText = $jsonMatches[1];
$JSON = json_decode( "{" . $jsonText[0] . "}" );
// Now we have the JSON, let's look for the size and
// colour dimensions. If we have those, we can attempt
// to map our size and colour to the dimensions in the
// data.
$size_key = null;
$colour_key = null;
if( array_key_exists( 'dimensions', $JSON ) )
{
foreach( $JSON->dimensions as $key => $dimension )
{
$this->log( ERROR_LEVEL_DEBUG, $dimension );
if( stristr( $dimension, 'color' ) ) $colour_key = $key;
if( stristr( $dimension, 'size' ) ) $size_key = $key;
}
if( !is_null( $colour_key ) && !is_null( $size_key ) )
{
foreach( $JSON->dimensionValuesDisplayData as $ASIN => $dimensions )
{
$this->log( ERROR_LEVEL_DEBUG, "Size: " . $dimensions[$size_key] . "; Color: " . $dimensions[$colour_key] );
if( $this->isLike( $colour, $dimensions[$colour_key] )
&& $this->isLike( $size, $dimensions[$size_key] ) )
{
$foundASIN = true;
break;
}
}
if( $foundASIN )
{
$this->log( ERROR_LEVEL_DEBUG, "Found ASIN " . $ASIN );
$this->writeToOutputFile( $SKU, $ASIN );
} else
{
$this->log( ERROR_LEVEL_DEBUG, "Unable to locate correct size " . $size . " and colour " . $colour . "." );
}
} else
{
$this->log( ERROR_LEVEL_DEBUG, "Unable to find ASIN for product - unable to locate size and product dimensions." );
$this->writeToErrorFile( $line, $SKU, "Match Found but could not locate size and colour dimensions." );
}
} else
{
$this->log( ERROR_LEVEL_DEBUG, "Dimension key array not found in JSON where expected." );
}
}
}
}
}
if( !$foundASIN )
{
// No variations, so look in the main body. The "add to cart"
// funcationality references the ASIN directly, so we can use that.
$this->log(ERROR_LEVEL_DEBUG, "ASIN not found in javascript; searching for single product ASIN.");
$ASINElement = $itemDOM->find("input[id='ASIN']", 0);
if( is_null( $ASINElement ) )
{
$this->log( ERROR_LEVEL_DEBUG, "Unable to find ASIN for product - field not found." );
$this->writeToErrorFile( $line, $SKU, "Match Found but ASIN Not Present" );
} else
{
// Look for matches on colour and size. If they're not given
// or not present on the Amazon page we declare success.
if( $this->colourMatch( $itemDOM, $colour )
&& $this->sizeMatch( $itemDOM, $size ) )
{
$ASIN = $ASINElement->getAttribute("value");
$this->log( ERROR_LEVEL_DEBUG, "Found ASIN " . $ASIN );
$this->writeToOutputFile( $SKU, $ASIN );
$foundASIN = true;
} else
{
$this->log( ERROR_LEVEL_DEBUG, "Unable to find ASIN for product - size or colour mismatch." );
$this->writeToErrorFile( $line, $SKU, "Match Found but failed on size or colour mismatch." );
}
}
}
return $foundASIN;
}
private function colourMatch( $itemDOM, $colour )
{
// No need to match if we don't have a colour.
if( $colour == "" ) return true;
// No need to match if colour isn't given on the page.
$colourElement = $itemDOM->find("div[id='variation_color_name]", 0);
if( is_null( $colourElement ) ) return true;
$colourText = $colourElement->find("span[class='selection']");
if( is_null( $colourText ) ) return true;
$this->log( ERROR_LEVEL_DEBUG, "Looking for a colour match: " . $colour . " to " . $colourText->innertext );
if( stristr( $colour, $colourText->innertext ) ) return true;
return false;
}
private function sizeMatch( $itemDOM, $size )
{
// No need to match if we don't have a size.
if( $size == "" ) return true;
// No need to match if size isn't given on the page.
$sizeElement = $itemDOM->find("div[id='variation_size_name]", 0);
if( is_null( $sizeElement ) ) return true;
$sizeText = $sizeElement->find("span[class='selection']")->innertext;
$this->log( ERROR_LEVEL_DEBUG, "Looking for a size match: " . $size . " to " . $sizeText );
if( stristr( $size, $sizeText ) ) return true;
return false;
}
private function loadSearch($searchTerms)
{
// Format the search URL with the base URL and search terms modified
// to fit the format required by Amazon.
$searchURL = BASE_URL . urlencode(preg_replace("/ /", "+", trim($searchTerms)));
return $this->getDOMFromURL($searchURL);
}
private function getDOMFromURL($url)
{
$start = time();
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Set curl to return the data instead of printing it to the browser.
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec($ch);
curl_close($ch);
$dom = new simple_html_dom( $data );
$finish = time();
$elapsed = $finish - $start;
$this->log( ERROR_LEVEL_DEBUG, "HTML Query elapsed: " . $elapsed );
return $dom;
}
private function cleanAllDOM(&$items,$leave = '')
{
foreach($items as $id => $item){
if($leave && ((!is_array($leave) && $id == $leave) || (is_array($leave) && in_array($id,$leave)))) continue;
if($id != 'GLOBALS'){
if(is_object($item) && ((get_class($item) == 'simple_html_dom') || (get_class($item) == 'simple_html_dom_node'))){
$items[$id]->clear();
unset($items[$id]);
}else if(is_array($item)){
$first = array_shift($item);
if(is_object($first) && ((get_class($first) == 'simple_html_dom') || (get_class($first) == 'simple_html_dom_node'))){
unset($items[$id]);
}
unset($first);
}
}
}
}
private function isLike( $string1, $string2, $minimumMatch = MINIMUM_MATCH_LEVEL )
{
similar_text( strtoupper( $string1 ), strtoupper( $string2 ), $matchLevel );
if( $matchLevel >= $minimumMatch ) return true;
return false;
}
private function log($level, $message)
{
if( $level <= DEBUG_LEVEL)
print( ERROR_LEVELS[$level] . ": " . $message . "\r\n" );
if( $level == ERROR_LEVEL_FATAL ) die();
}
private function writeToErrorFile( $line, $SKU, $message )
{
$this->errorCount ++;
fputcsv( $this->errorFile, [$line, $SKU, $message] );
}
private function writeToOutputFile( $SKU, $ASIN )
{
$this->successCount ++;
fputcsv( $this->outputFile, [$SKU, $ASIN] );
}
private function cleanString( $string )
{
$string = strip_tags( html_entity_decode( $string ) );
$string = trim( $string );
$string = trim( $string, "\"" );
$string = trim( $string );
$string = preg_replace('/\s\s+/', ' ', $string );
return $string;
}
private function startsWithPartofWord( $name, $word )
{
// The general rule is that the first word of the name may start with
// part of the SKU, so we extract the first word from the name and look
// for it in the SKU. The string "$name" should be cleaned before
// calling this function.
$firstWord = current( explode( " ", $name ) );
if( stripos( $word, $firstWord ) == 0 )
return true;
return false;
}
private function deleteFirstWord( $string )
{
$elements = explode( " ", $string );
array_shift( $elements );
$shortString = trim( implode( " ", $elements ) );
// Also trim off any starting hyphen and spaces.
$shortString = trim( $shortString, "-" );
$shortString = trim( $shortString );
return $shortString;
}
}
if( $argc < 4 ) die( "Usage: php scraper.php inputFile outputFile errorFile" );
$scraper = new Scraper($argv[1], $argv[2], $argv[3]);
$scraper->scrape();
?>