简单的HTML DOM从<div>标记获取属性

时间:2019-03-18 14:33:15

标签: web-scraping simple-html-dom

我正在尝试获取的'tel'属性,但无法获取。

<div class="horizontal-center" ng-class="classActive()" ng-click="clicked($event)" id="7076213" account-id="536423" user-id="651772" event-label="listing-detail" event-action="phone-number-viewed-in-sideways" tel="8503128272" action-type="DetailPhone">

我正在尝试使用这些代码。 1。

foreach ($html1->find('div.horizontal-center') as $phone) {
 echo   $ilanlar['phone'] = $phone->{'tel'};
}

2。

foreach ($html1->find('div.horizontal-center') as $phone) {
 echo   $ilanlar['phone'] = $phone->getattribute('tel');
}

1 个答案:

答案 0 :(得分:0)

include('simple_html_dom.php');


$html1 = str_get_html('<div class="horizontal-center" ng-class="classActive()" ng-click="clicked($event)" id="7076213" account-id="536423" user-id="651772" event-label="listing-detail" event-action="phone-number-viewed-in-sideways" tel="8503128272" action-type="DetailPhone">');

foreach ($html1->find('div.horizontal-center') as $phone) {
    echo $ilanlar['phone'] = $phone->{'tel'};
}

echo '<br>'; 

foreach ($html1->find('div.horizontal-center') as $phone) {
    echo $ilanlar['phone'] = $phone->getattribute('tel');
}

对我来说很好。输出为:

8503128272<br>8503128272