0

为什么我的脚本不会返回 id 为“pp-featured”的 div?

<?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();
?>
4

2 回答 2

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;

?>
于 2011-11-21T06:10:25.097 回答
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";
}
于 2013-06-11T05:37:27.457 回答