Skip to content

Instantly share code, notes, and snippets.

@mouson
Created September 14, 2017 09:39
Show Gist options
  • Save mouson/28dc4eadab1639808108ab332efca851 to your computer and use it in GitHub Desktop.
Save mouson/28dc4eadab1639808108ab332efca851 to your computer and use it in GitHub Desktop.
蘋果官網整修品頁面新品取得機器人
<?php
require_once __DIR__ . '/../vendor/autoload.php';
use Goutte\Client as Goutte_Client;
$client = new Goutte_Client();
$records_path = __DIR__ . '/../logs/records.log';
$apple_url = 'https://www.apple.com';
$country_code = '/tw';
$specialdeals_url = '/shop/browse/home/specialdeals/mac';
$request_uri = "{$apple_url}{$country_code}{$specialdeals_url}";
$content_selector = 'div#primary div.box.refurb-list > div.box-content';
$crawler = $client->request('GET', $request_uri)
->filter($content_selector);
$new_items = [];
$products_selector = 'table > tbody > tr';
$crawler->filter($products_selector)
->each(function ($node) use (&$new_items) {
$item = trim($node->filter('td.specs > h3 > a')->text());
$price = trim(
substr(trim($node->filter('td.purchase-info > p')->text()), -10)
);
$url = $node->filter('td.purchase-info > a')->attr('href');
$url_attributes = explode('/', $url);
$product_id = trim($url_attributes[4]);
$new_items[$product_id] = [
'Item' => $item,
'Price' => $price,
'Url' => $url,
];
});
$new_record = json_encode($new_items);
$previous_record = file_get_contents($records_path);
if (
$previous_record !== false
&& $new_record != $previous_record
) {
send_alert($new_items, json_decode($previous_record, true));
}
file_put_contents($records_path, $new_record);
function send_alert($new, $previous) {
// do something
print_r(array_diff_key($new, $previous));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment