首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >php前沿问题

php前沿问题
EN

Stack Overflow用户
提问于 2011-07-15 12:56:36
回答 2查看 87关注 0票数 1

我正在使用simple_html_dom_helper,所以做一些屏幕抓取,并遇到一些错误。

第二个foreach运行4次(自sizeof($pages) == 4以来),而它只运行一次。我从一个示例脚本中获得了这段代码,其中table.result-liste在页面上多次出现。在我的例子中,它只发生过一次,所以我不需要预先处理。print_r($data) 打印出相同的东西4次,没有必要这样做。

再往下看,我试图在没有预见的情况下做同样的事情,但是它只是打印出no,所以似乎有一个不同的响应,并且不知道为什么。

代码语言:javascript
复制
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 ";           
        }
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-07-15 13:00:38

尽管table[class=result-liste]只在页面上发生一次,但这个find语句正在查找表的行的<tr>元素。因此,除非您的表只有一行,否则您将需要这个foreach

代码语言:javascript
复制
$p_html->find("table[class=result-liste] tr")
票数 2
EN

Stack Overflow用户

发布于 2011-07-15 13:18:22

您的代码

代码语言:javascript
复制
        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);
            }                                               
        }

用我的代码替换上面的代码

代码语言:javascript
复制
        $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);
            }                                               

试试这个。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6707272

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档