【问题标题】:Simple HTML DOM - traversing html简单的 HTML DOM - 遍历 html
【发布时间】:2013-08-26 17:56:42
【问题描述】:

我正在使用简单的 HTML DOM 解析器 - http://simplehtmldom.sourceforge.net/manual.htm 我正在尝试从记分牌页面中抓取一些数据。下面的示例展示了我拉取“Akron Rushing”表的 HTML。

$tr->find('td', 0) 的第一列中,有一个超链接。如何提取此超链接?使用$tr->find('td', 0')->find('a') 似乎不起作用。

另外:我可以为每张桌子写条件(传球、冲球、接球等),但有没有更有效的方法呢?我对这方面的想法持开放态度。

include('simple_html_dom.php');
$html = file_get_html('http://espn.go.com/ncf/boxscore?gameId=322432006');

$teamA['rushing'] = $html->find('table.mod-data',5);

foreach ($teamA as $type=>$data) {
  switch ($type) {
    # Rushing Table
    case "rushing":
       foreach ($data->find('tr') as $tr) {
        echo $tr->find('td', 0);    // First TD column (Player Name)
        echo $tr->find('td', 1);    // Second TD Column (Carries)
        echo $tr->find('td', 2);    // Third TD Column (Yards)
        echo $tr->find('td', 3);    // Fourth TD Column (AVG)
        echo $tr->find('td', 4);    // Fifth TD Column (TDs)
        echo $tr->find('td', 5);    // Sixth TD Column (LGs)
        echo "<hr />";
        }
   }
}

【问题讨论】:

    标签: php dom simple-html-dom tree-traversal


    【解决方案1】:

    在您的情况下,find('tr') 返回 10 个元素,而不是预期的 7 行。

    此外,并非所有名称都有与之关联的链接,当链接不存在时尝试检索链接可能会返回错误。

    因此,这是您的代码修改后的工作版本:

    $url = 'http://espn.go.com/ncf/boxscore?gameId=322432006';
    
    $html = file_get_html('http://espn.go.com/ncf/boxscore?gameId=322432006');
    
    $teamA['rushing'] = $html->find('table.mod-data',5);
    
    foreach ($teamA as $type=>$data) {
      switch ($type) {
        # Rushing Table
        case "rushing":
            echo count($data->find('tr')) . " \$tr found !<br />";
    
            foreach ($data->find('tr') as $key => $tr) {
    
                $td = $tr->find('td');
    
                if (isset($td[0])) {
                    echo "<br />";
                    echo $td[0]->plaintext . " | ";         // First TD column (Player Name)
    
                    // If anchor exists
                    if($anchor = $td[0]->find('a', 0))
                        echo $anchor->href;                 // href
    
                    echo " | ";
    
                    echo $td[1]->plaintext . " | ";     // Second TD Column (Carries)
                    echo $td[2]->plaintext . " | ";     // Third TD Column (Yards)
                    echo $td[3]->plaintext . " | ";     // Fourth TD Column (AVG)
                    echo $td[4]->plaintext . " | ";     // Fifth TD Column (TDs)
                    echo $td[5]->plaintext;             // Sixth TD Column (LGs)
                    echo "<hr />";
                }
    
            }
       }
    }
    

    如您所见,可以使用这种格式$tag-&gt;attributeName 重新调整属性。在您的情况下,attributeNamehref

    注意事项:

    处理 find 的错误是个好主意,因为知道在没有找到任何东西时它会返回“False”

    $td = $tr->find('td');
    
    // Find suceeded
    if ($td) {
        // code here
    }
    else
      echo "Find() failed in XXXXX";
    

    PHP Simple HTML DOM Parser 已经知道 php5 的内存泄漏问题,所以不要忘记在不再使用 DOM 对象时释放内存:

    $html = file_get_html(...);
    
    // do something... 
    
    $html->clear(); 
    unset($html);
    
    Source: http://simplehtmldom.sourceforge.net/manual_faq.htm#memory_leak
    

    【讨论】:

      【解决方案2】:

      根据文档,您应该能够为嵌套元素链接选择器。

      这是他们给出的例子:

      // Find first <li> in first <ul>    
      $e = $html->find('ul', 0)->find('li', 0);
      

      我能看到的唯一区别是它们在第二个查找中包含索引。尝试添加它,看看它是否适合你。

      【讨论】:

        猜你喜欢
        • 2012-11-06
        • 2020-04-18
        • 1970-01-01
        • 2015-09-30
        • 2023-03-04
        • 2012-06-24
        • 1970-01-01
        • 2014-09-20
        相关资源
        最近更新 更多