PHP简单DOMDocument抓取排除td类

时间:2017-12-29 04:08:39

标签: php html web-scraping domdocument

我只是想让所有<td>元素数据驻留在<tr>个元素中。我的问题是因为表格结构我试图抓取我需要排除属性COLLSPAN的所有元素,即<td collspan = 12> 获取表数据很简单,从下面的代码可以看出,但由于表结构,我需要排除所有collspan属性。

<?php

$html = file_get_contents('http://www.superxv.com/fixtures/'); //get the html returned from the following url

$game_doc = new DOMDocument();
libxml_use_internal_errors(TRUE); //disable libxml errors
if(!empty($html)) { //if any html is actually returned
    $game_doc->loadHTML($html);
    libxml_clear_errors(); //remove error
    $xpath = new DOMXPath($game_doc);

    // Modify the XPath query to match the content
    foreach ($xpath->query('//table')->item(0)->getElementsByTagName('tr') as $rows) {
        $cells = $rows->getElementsByTagName('td');
        //$cells2 = $rows->getElementsByTagName('th');
        echo '<pre>';
         //@ signs are added due to table structure
        //Get scrapped columns
        echo $dayDateBye[] = $cells->item(0)->textContent;
        echo $homeTeam[] = $cells->item(1)->textContent;
        echo $awayTeam[] = $cells->item(2)->textContent;
        echo $venue[] = $cells->item(3)->textContent;
        echo $timeGMT[] = $cells->item(5)->textContent;
        echo $timeZA[] = $cells->item(10)->textContent;
        echo '</pre>';
    }
}

在这里,您可以看到表格结构,它显示了5个奇数行的灯具,然后在新周开始时更改结构。我可以识别出跳过这种结构变化的元素是<td collspan = 12>元素。这使得它变得棘手,因为TD元素没有类名,只有用于识别它的元素。

enter image description here

enter image description here

任何意见都表示赞赏。

2 个答案:

答案 0 :(得分:3)

您可以按标签的长度跳过那些

<?php

$html = file_get_contents('http://www.superxv.com/fixtures/'); //get the html returned from the following url

$game_doc = new DOMDocument();
libxml_use_internal_errors(TRUE); //disable libxml errors
if(!empty($html)) { //if any html is actually returned
    $game_doc->loadHTML($html);
    libxml_clear_errors(); //remove error
    $xpath = new DOMXPath($game_doc);

    // Modify the XPath query to match the content
    foreach ($xpath->query('//table')->item(0)->getElementsByTagName('tr') as $rows) {
        $cells = $rows->getElementsByTagName('td');
        if( $cells->length > 1 ){
            //$cells2 = $rows->getElementsByTagName('th');
            echo '<pre>';
             //@ signs are added due to table structure
            //Get scrapped columns
            echo $dayDateBye[] = $cells->item(0)->textContent;
            echo $homeTeam[] = $cells->item(1)->textContent;
            echo $awayTeam[] = $cells->item(2)->textContent;
            echo $venue[] = $cells->item(3)->textContent;
            echo $timeGMT[] = $cells->item(5)->textContent;
            echo $timeZA[] = $cells->item(10)->textContent;
            echo '</pre>';
        }
    }
}

?>

答案 1 :(得分:1)

使用xpath排除colspan属性

的元素

所以而不是:

$cells = $rows->getElementsByTagName('td');

使用:

$cells = $xpath->query('td[not(@colspan)]', $rows);