也有类似的线程可用。我看着他们,试图得到。在这里我可以得到地址值,但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