我正在使用simple_html_dom_helper,所以做一些屏幕抓取,并遇到一些错误。
第二个foreach运行4次(自sizeof($pages) == 4以来),而它只运行一次。我从一个示例脚本中获得了这段代码,其中table.result-liste在页面上多次出现。在我的例子中,它只发生过一次,所以我不需要预先处理。print_r($data) 打印出相同的东西4次,没有必要这样做。
再往下看,我试图在没有预见的情况下做同样的事情,但是它只是打印出no,所以似乎有一个不同的响应,并且不知道为什么。
foreach( $pages as $page )
{
$p = $this->create_url($codes[0], $price, $page); //pass page number along
$p_html = file_get_html($p);
$row = $p_html->find("table[class=result-liste] tr");
//RUNS OK BUT NO NEED TO DO IT FOUR TIMES.
//CLASS RESULT-LISTE ONLY OCCURS ONCE ANYWAY
foreach( $p_html->find("table[class=result-liste] tr") as $row)
{
//grab only rows where there is a link
if( $row->find('td a') )
{
$d_price = $this->get_price($row->first_child());
$d_propid = $this->get_prop_id($row->outertext);
$data = array(
"price" => $d_price,
"prop_id" => $d_propid
);
print_r($data);
}
}
//MY ATTEMPT TO AVOID THE SECOND FOREACH DOES NOT WORK ...
$row = $p_html->find("table[class=result-liste] tr");
if( is_object($row) && $row->find('td a')) print "yes ";
else print "no ";
}发布于 2011-07-15 13:00:38
尽管table[class=result-liste]只在页面上发生一次,但这个find语句正在查找表的行的<tr>元素。因此,除非您的表只有一行,否则您将需要这个foreach。
$p_html->find("table[class=result-liste] tr")发布于 2011-07-15 13:18:22
您的代码
foreach( $p_html->find("table[class=result-liste] tr") as $row)
{
//grab only rows where there is a link
if( $row->find('td a') )
{
$d_price = $this->get_price($row->first_child());
$d_propid = $this->get_prop_id($row->outertext);
$data = array(
"price" => $d_price,
"prop_id" => $d_propid
);
print_r($data);
}
}用我的代码替换上面的代码
$asRow = $p_html->find("table[class=result-liste] tr");
$row = $asRow[0];
//grab only rows where there is a link
if( $row->find('td a') )
{
$d_price = $this->get_price($row->first_child());
$d_propid = $this->get_prop_id($row->outertext);
$data = array(
"price" => $d_price,
"prop_id" => $d_propid
);
print_r($data);
} 试试这个。
https://stackoverflow.com/questions/6707272
复制相似问题