简单的HTML DOM:注意 - >尝试获取非对象的属性

时间:2013-01-25 18:51:02

标签: php html web-scraping simple-html-dom

当使用简单的html dom来抓取网站时,我收到了php通知。显示2个通知,当使用print_r函数显示时,下面呈现的所有内容都很完美。

网站表格结构如下:

    <table class=data schedTbl>
        <thead>
            <tr>
                <th>DATA</th>
                <th>DATA</th>
                <th>DATA</th>
                etc....
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>
                    <div class="class1">DATA</div>
                    <div class="class2">SAME DATA AS PREVIOUS DIV</div>
                </td>
                <td>DATA</td>
                <td>DATA</td>
                etc....
            </tr>
            <tr>
                <td>
                    <div class="class1">DATA</div>
                    <div class="class2">SAME DATA AS PREVIOUS DIV</div>
                </td>
                <td>DATA</td>
                <td>DATA</td>
                etc....
            </tr>
            <tr>
                <td>
                    <div class="class1">DATA</div>
                    <div class="class2">SAME DATA AS PREVIOUS DIV</div>
                </td>
                <td>DATA</td>
                <td>DATA</td>
                etc....
            </tr>
            etc....
        </tbody>
    </table>

下面的代码用于查找表[class = data schedTbl]中的所有tr。我有一个tbody选择器,但它似乎不关注这个选择器,因为它仍然选择thead中的tr。

    include('simple_html_dom.php');

    $articles = array();

    getArticles('www.somesite.com');

    function getArticles($page) {
         global $articles;

         $html = new simple_html_dom();
         $html->load_file($page);

         $items = $html->find('table[class=data schedTbl] tbody tr');  

         foreach($items as $post) {

             $articles[] = array($post->children(0)->first_child(0)->plaintext,//0 -- GAME DATE
                        $post->children(1)->plaintext,//1 -- AWAY TEAM
                        $post->children(2)->plaintext);//2 -- HOME TEAM

         }

    }

所以,我相信通知来自thead中的tr,因为我正在呼叫第一个td的第一个孩子,它只有一个记录。原因之二是实际上有两个表在主体中具有相同的数据结构。

同样,我相信有两种方法可以解决这个问题:

1)可能是EASIEST(修复查找选择器以便TBODY正常工作,只选择tbodies中的tds)

2)找出一种不需要first_child过滤器的方法吗?

如果您想要我收到的print_r($ articles)输出的快照,请告诉我。

提前感谢您提供的任何帮助!

此致

比尔C.

1 个答案:

答案 0 :(得分:0)

只需注释simple_html_dom.php

中的第695行
if ($m[1]==='tbody') continue;

然后它应该读取tbody。