2012-03-24 24 views
0

我是新來的PHP,並試圖從網站上刮取數據我使用正則表達式,但在div中查找內容出租和詳細信息是一個問題,這裏是我的代碼。有人可以幫我嗎?刮和div

<?php 
header('content-type: text/plain'); 
$contents= file_get_contents('http://www.hassconsult.co.ke/index.php?option=com_content&view=article&id=22&Itemid=29'); 
$contents = preg_replace('/\s(1,)/','',$contents); 
$contents = preg_replace('/&nbsp;/','',$contents); 

//print $contents."\n"; 
$records = preg_split('/<span class="style8"/',$contents); 

for ($ix=1; $ix < count($records); $ix++){ 
$tmp = $records[$ix]; 

preg_match('/href="(.*?)"/',$tmp, $match_url); 
preg_match('/>(.*?)<\/span>/',$tmp,$match_name); 
preg_match('/<div[^>]+class ?= ?"style10"[^>]*>(\s*(<div.*(?2).*<\/div>\s*)*)<\/div>/Us',$tmp,$match_rental);//error is here 
print_r($match_url); 
print_r($match_name); 
print_r($match_rental); 
print $tmp."\n"; 
exit(); 
} 
//print count($records)."\n"; 
//print_r($records); 
//if ($contents===false) 
//print 'FALSE'; 
//print_r(htmlentities($contents)); 

?> 

這裏是內容

>HILLVIEW CROSSROADS4 BED HOUSE</span></div></td> 
       </tr> 
       <tr> 
        <td width="57%" style="padding-left:20px;"><div align="left" class="style10" style="color:#007AC7;"> 
         <div align="left"> 
              Rental; 
         USD      4,500 
         </div> 
        </div></td> 
        <td width="43%" align="right"><div align="right" class="style10" style="color:#007AC7;"> 
         <div align="right"> 

         No.    
         834 

         </div> 
        </div></td> 
       </tr> 
       <tr> 
        <td colspan="2" style="padding-left:20px;color:#000000;"> 
        <div align="justify" style="font-family:Arial, Helvetica, sans-serif;font-size:11px;color:333300;">Artistically designed 4 bed (all 
ensuite) house on half acre of well-tended gardens. Lounge with fireplace opening to terrace, opulent master suite, family room, study. Good finishes, SQ, carport, extra water storage 
and generator.        <a href="/index.php?option=com_content&amp;view=article&amp;id=27&amp;Itemid=74&amp;send=5&amp;ref_no=834/II&amp;t=2">....Details</a>    </div></td> 
       </tr> 
      </table></td> 
      </tr> 
</table> 
<br> 
+0

爲什麼你使用正則表達式來解析HTML? PHP有多個可用的HTML解析器,它可以處理所有類型的正則表達式不能使用的東西。 HTML解析器知道哪些構造在HTML和XHTML的哪些版本中是有效的,並且使用doctype來確定該頁面正在使用哪個版本。 – 2012-03-24 19:58:46

+0

請給我鏈接到一個教程將高度讚賞我有點新 – user1207576 2012-03-25 05:15:36

回答

2

的樣品該網站不具備良好的CSS選擇器,但它仍然不是很難與XPath來得到它:

$dom = new DOMDocument(); 
@$dom->loadHTMLFile('http://www.hassconsult.co.ke/index.php?option=com_content&view=article&id=22&Itemid=29'); 
$xpath = new DOMXPath($dom); 

foreach($xpath->query("//div[@id='ad']/table") as $table) { 
    // title 
    echo $xpath->query(".//span[@class='style8']", $table)->item(0)->nodeValue . "\n"; 
    // price 
    echo $xpath->query(".//div[@class='style10']/div", $table)->item(0)->nodeValue . "\n"; 
    // description 
    echo $xpath->query(".//div[@align='justify']", $table)->item(0)->nodeValue . "\n"; 
} 
+0

你知道我可以遍歷到下一個頁面或細節,因爲在更多的細節,我需要的圖像,並從地圖經緯度做XPath支持此?謝謝! – user1207576 2012-03-25 05:32:16

+0

我推薦閱讀一些xpath教程並自己嘗試。如果你遇到困難,你可以用xpath標籤發佈一個新問題,你可能會得到一個很好的答案。 – pguardiario 2012-03-25 06:24:14

+0

好感謝最後一個問題我怎麼標題添加回聲$ xpath->查詢( 「./跨度[@類= '美麗人生']」,$表) - >項目(0) - >的nodeValue;在foreach下面,並返回一個錯誤,試圖獲取名稱。 – user1207576 2012-03-25 06:31:13