您可以輕鬆實現使用DomDocument解析HTML並使用xpath查詢訪問所需的數據。
在這裏你有一個基本的例子,從類'領域項目甚至'從div獲取數據。
<?php
$html = file_get_contents("https://raceskis.ski-depot.com/products/skis");
$doc = new DOMDocument();
$doc->loadHTML($html);
$xpath = new DomXPath($doc);
$nodeList = $xpath->query("//div[@class='field-item even']");
print_r($nodeList);
foreach($nodeList as $node){
echo "<p>" . $node->nodeValue . "</p>";
}
輸出是:
DOMNodeList Object
(
[length] => 97
)
1
0
Fischer RC4 Superior RC w/bindings Skis 2014
In stock
$900.00
$399.00
1
0
Fischer Worldcup SC w/bindings Race Skis 2015
In stock
$1,250.00
$499.00
1
0
現在你可以過濾這些信息只拿到了價格。
$pricesArray = [];
foreach($nodeList as $node){
$nodeValue = $node->nodeValue;
// If $nodeValue has content and its first character is $
if($nodeValue && $nodeValue[0] == '$'){
$pricesArray[] = $nodeValue;
}
}
echo "<pre>";
print_r($pricesArray);
echo "</pre>";
輸出是:
Array
(
[0] => $900.00
[1] => $399.00
[2] => $1,250.00
[3] => $499.00
[4] => $550.00
[5] => $275.00
[6] => $900.00
[7] => $375.00
[8] => $1,200.00
[9] => $599.00
[10] => $1,065.00
[11] => $499.00
[12] => $550.00
[13] => $275.00
[14] => $1,125.00
[15] => $549.00
[16] => $1,125.00
[17] => $549.00
[18] => $1,250.00
[19] => $549.00
[20] => $550.00
[21] => $275.00
[22] => $1,065.00
[23] => $599.00
)
檢查PHP訪問目標網站。 '$ html'應該有價值。 – Mohammad