2016-06-06 80 views
0

我想從這個html中聚合俱樂部電話號碼。不正確的xpath查詢

這裏是我的HTML:

<div id="content"> 
    <div class="container"> 
    <div class="padded"> 
     <ul class="state-list"> 
     <ul class="state-city-list"> 
      <li class="jumper"> 
     <div class="clubinfo"> 
      <div class="clubaddress">123 West Palm Blvd</div> 
      <div class="clubphone">5555551123</div> 
     </div> 
      </li> 
     </ul>      
     <ul class="state-city-list"> 
      <li class="jumper"> 
     <div class="clubinfo"> 
      <div class="clubaddress">456 West Palm Blvd</div> 
      <div class="clubphone">5555555456</div> 
     </div> 
      </li> 
     </ul> 
     <ul class="state-city-list"> 
      <li class="jumper"> 
     <div class="clubinfo"> 
      <div class="clubaddress">789 West Palm Blvd</div> 
      <div class="clubphone">5555555789</div> 
     </div> 
      </li> 
     </ul> 
     <ul class="state-city-list"> 
      <li class="jumper"> 
     <div class="clubinfo"> 
      <div class="clubaddress">213 West Palm Blvd</div> 
      <div class="clubphone">5555555213</div> 
     </div> 
      </li> 
     </ul> 
     </ul> 
    </div> 
    </div> 
</div> 

這是我的PHP代碼:

$htmlfile='phonecontents'; 
    $xml=new DOMDocument(); 
    $xml->preserveWhiteSpace=false; 
    $xml->loadHTMLFile($htmlfile); 
    $xpath = new DOMXpath($xml); 
    //$elm = $xpath->query('/div[@id="content"]/div[@class="container"]/div[@class="padded"]/ul[@class="state-list"]/li[@class="jumper"]/div[@class="clubinfo"]/div[@class="clubphone"]'); 
    $elm = $xpath->query('/div[@id="content"]/div[@class="clubinfo"]/div[@class="clubphone"]'); 
    if(!is_null($elm)){ 
     var_dump($elm); 
     foreach($elm as $el){ 
     echo $el->nodeValue."\n"; 
     } 
    } else { 
     echo "NO DATA FOUND \n"; 
    } 

我不太清楚,因爲我是新來的XPath,但我不能讓我的數據正確。如果你看看php代碼,你可以看到我用兩個不同的查詢嘗試,似乎沒有工作。

+1

'// div [@ class =「clubphone」]'應該可以工作。你需要在開始時使用'// //'來獲取所有的html,並用xpath檢查它 –

回答

0

嘗試以下操作:

$elm = $xpath->query('//div[@id="content"]//div[@class="clubinfo"]/div[@class="clubphone"]'); 

使用一個斜線意味着,使用兩條斜線的意思是「任何後代」,「直子」。