2011-11-20 31 views
0

爲什麼我的腳本不會返回ID爲「pp-featured」的div?PHP簡單的HTML DOM解析器不會返回任何東西

<?php 
# create and load the HTML 
include('lib/simple_html_dom.php'); 
$html = new simple_html_dom(); 
$html->load("http://maps.google.com/maps/place?cid=6703996311168776503&q=hills+garage&hl=en&view=feature&mcsrc=google_reviews&num=20&start=0&ved=0CFUQtQU&sa=X&ei=sCq_Tr3mJZToygTOmuCGCg"); 

$ret = $html->find('div[id=pp-featured]'); 

# output it! 
echo $ret->save(); 
?> 
+0

什麼'回聲$ RET;'收益呢? –

+0

只是單詞數組。 – Kelbizzle

+0

通常在CSS中,選擇器將是'div [id =「pp-featured」]',不確定您使用的這個類是否嚴格。但是如果你正在尋找一個ID,爲什麼不使用'div#pp-featured'?這個班不支持嗎? – animuson

回答

0

這讓我在路上。謝謝你的幫助。

<?php 

include_once 'lib/simple_html_dom.php'; 

$url = "http://maps.google.com/maps/place?cid=6703996311168776503&q=hills+garage&hl=en&view=feature&mcsrc=google_reviews&num=20&start=0&ved=0CFUQtQU&sa=X&ei=sCq_Tr3mJZToygTOmuCGCg"; 

$html = file_get_html($url); 

$ret = $html->find('div[id=pp-reviews]'); 

foreach($ret as $story) 
    echo $story; 

?> 
0

庫總是返回一個數組,因爲可能有多個項目與選擇器匹配。

如果您希望只有一個您應該檢查以確保您分析的頁面按預期行事。

建議解決方案:

<?php 

include_once 'lib/simple_html_dom.php'; 

$url = "http://maps.google.com/maps/place?cid=6703996311168776503&q=hills+garage&hl=en&view=feature&mcsrc=google_reviews&num=20&start=0&ved=0CFUQtQU&sa=X&ei=sCq_Tr3mJZToygTOmuCGCg"; 

$html = file_get_html($url); 

$ret = $html->find('div[id=pp-reviews]'); 
if(count($ret)==1){ 
echo $ret[0]->save(); 
} 
else{ 
echo "Something went wrong"; 
} 
+0

獲取此錯誤:致命錯誤:調用成員函數find()在非對象中... – Srinivas08