使用html dom和类名来刮取数据

时间:2014-04-29 06:22:31

标签: php dom

我在这里试图从网页上获取地址和电话号码。这是代码,它不会给出任何错误但也不会给出任何结果。

有什么不对吗?

我正在尝试从页面中获取address,一个imagephone

这是代码:

include_once('simple_html_dom.php');
function getData($url)
{
    print("$url\n");
    $root = new stdClass();
    $items = array();
    $html = file_get_html($url);
    if($html) {
        $containers = $html->find('div.mapbox div.mapbox-text strong.street-address address.address');
        foreach($containers as $container) {
            $comments = $container->find('address.address span');
            $item = new stdClass();
            foreach($comments as $comment) {
                $address.= $comment->itemprop; //append the content of each span
            }
            echo $address;

            $getphone = $container->find('span.biz-phone');
            $phone = $getphone->itempro;
        }   

        $Imgcontainers = $html->find('div.js-photo photo photo-1 div.showcase-photo-box img.a la beverly sills');
        echo $Imgcontainers->img;
    }
}

$url = 'http://www.yelp.com/biz/locanda-san-francisco?start='.$i.'';
$root = getData($url);

1 个答案:

答案 0 :(得分:1)

<address>标记中没有address类,因此$containers返回空。在if条件

中使用以下代码
$containers = $html->find('div.mapbox div.mapbox-text strong.street-address address');
foreach($containers as $container) {
   $comments = $container->find('span');
   $item = new stdClass();
   foreach($comments as $comment) {
       $address.= $comment->itemprop; //append the content of each span
   }
   echo $address;

   $getphone = $container->find('span.biz-phone');
   $phone = $getphone->itempro;
}
$Imgcontainers = $html->find('div.js-photo photo photo-1 div.showcase-photo-box img.a la beverly sills');
echo $Imgcontainers->img;
相关问题