也有類似的線程可用。我看着他們,試圖得到。在這裏我可以得到地址值,但phone
號碼和image url
不在。使用dom獲取屬性和標記值
我在哪裏犯錯?
function getData($url){
$containers1 = $html->find('div.mapbox div.mapbox-text strong.street-address address');
foreach($containers1 as $container)
{
$comments = $container->find('span');
$item = new stdClass();
foreach($comments as $comment)
{
$address.= $comment->plaintext; //append the content of each span
}
echo $address; // this gives correct result
}
$containers2 = $html->find('div.mapbox div.mapbox-text span.biz-phone');
$phone = $containers2->innertext;
echo "<br/>".$phone."<br/>"; // no result
$Imgcontainers = $html->find('div.js-photo photo photo-1 div.showcase-photo-box img');
echo $Imgcontainers->getAttribute('src'); // Fatal error: Call to a member function getAttribute() on a non-object
}
}
$url = 'http://www.yelp.com/biz/locanda-san-francisco?start=40';
$root = getData($url);
UPDATE
我加了這一點:
$Imgcontainers = $html->find('div.photo-1 img');
foreach($Imgcontainers as $cont){
$img[] = $cont->getAttribute('src');
}
echo $img[0];
相同的URL兩次?爲什麼兩次,雖然我們僅回顯第0個數組值?
是否使用簡單的HTML DOM解析器? –
只需使用一個獨特的類來讀取圖像標籤$ Imgcontainers = $ html-> find('div.photo-1 img'); – Manibharathi