0

我正在尝试从一些家得宝 URL 中抓取定价数据。我正在使用 simple_html_dom.php,可以在这里找到:https ://simplehtmldom.sourceforge.io/

我在弄清楚如何获取我需要的单个跨度类以获取我想要的数据时遇到问题。

这是检查元素的图像,其中包含我尝试访问的各个字段: https ://prnt.sc/qx5ujt

这是我到目前为止返回一个空数组的代码:

?<php
require 'simple_html_dom.php';

$dom = file_get_html('https://www.homedepot.com/p/Zinsco-20-Amp-1-1-2-in-2-Pole-Replacement-Thick-Circuit-Breaker-UBIZ220/100183119', false);

$answer = array();
if(empty($dom))
{
     echo "EMPTY";
     exit;
} 
$divClass = "";
$dollars = "";
$cents = "";
$i = 0;
foreach($dom->find('price__wrapper') as $divClass) 
{
foreach($divClass->find('span[class=price__dollars]') as $dollars) //dollars
{
     $answer[$i]['dollars'] = $dollars->plaintext;
}
foreach($divClass->find('span[class=price__cents]') as $cents) //cents
{
     $answer[$i]['cents'] = $cents->plaintext;
}
$i++;
}

print_r($answer); 
exit;
?>

4

1 回答 1

0

如果 HTML 看起来总是像屏幕截图中那样,那么您可以继续使用content父 span 元素的属性。

您可以像下面这样存档(未经测试的示例)。

// Get the parent span element with ID ajaxPrice
$element = $dom->find('span#ajaxPrice',0);
// Check if attribute "content" exists
if(isset($element->content)) {
    $price = $element->content; // This is the price as string value
    $priceFloat = floatval($price); // This is the price as float value

    // Splitting price in dollar and cent
    $price = explode('.', $price);
    $dollar = $price[0];
    $cent = $price[1];
}
于 2020-02-04T04:12:11.040 回答