2014-04-29 27 views
0

這裏我試圖從網頁獲取地址和電話號碼。這裏是沒有給出任何錯誤,但也沒有給出任何結果的代碼。使用html dom和類名颳去數據

有什麼問題嗎?

我想從頁面獲取address,1個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

<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; 
+0

感謝花花公子,但它給''streetAddressaddressLocalityaddressRegionpostalCode而不是值。看看有什麼問題 – user123

+1

使用'$ address。= $ comment-> plaintext;'或'$ address。= $ comment-> innertext;'這將返回文本。 – Manibharathi

+0

@Manirbharathi:謝謝,它給了地址與明文。我使用了純文本和innertext圖像和手機,但這兩個都沒有打印 – user123