如何使用简单html dom提取标签属性?

时间:2018-12-18 15:54:59

标签: php html

我试图使用simple_html_dom.php提取信息

该行如下所示:

<meta itemprop="openingHours" content="Mo,Tu,We,Th,Fr,Sa,Su 08:00-00:00">

我需要“ Mo,Tu,We,Th,Fr,Sa,Su 08:00-00:00”部分。

这是我到目前为止尝试过的:

$url="https://www1.shoppersdrugmart.ca/en/store-locator/store/668"; 
include ('../classes/simple_html_dom.php');
$html = file_get_html($url);

//this works fine
$eg = $html->find('dd[itemprop="telephone"]');
echo "Phone: ".$eg[0]->plaintext. "<br>";

//this does not work
$eg = $html->find('meta[itemprop="openingHours"]');
echo "openingHours: ". $eg['content']->plaintext. "<br>";

$oh_content=$html->find('meta[itemprop="openingHours"]')->attr("content");
echo $oh_content."*<br>";

$oh_content1=$html->find('meta[itemprop="openingHours"]')->content;
echo $oh_content1."*<br>";

2 个答案:

答案 0 :(得分:0)

$eg = $html->find('dd[itemprop="telephone"]');中,$eg是已过滤节点的数组,然后对于第二个find,它是 true

$eg = $html->find('meta[itemprop="openingHours"]');  
// $eg is array:
var_dump($eg[0]->content);

答案 1 :(得分:0)

万一有人需要,下面的代码可以工作:

$url="https://www1.shoppersdrugmart.ca/en/store-locator/store/668"; 
include ('../classes/simple_html_dom.php');
$html = file_get_html($url);

$eg = $html->find('dd[itemprop="telephone"]');
echo "Phone: ".$eg[0]->plaintext. "<br>";

$eg = $html->find('meta[itemprop="openingHours"]');
echo "openingHours: ". $eg[0]->content. "<br>";