Search tables by ID using simple HTML DOM Parser

Last year, I wrote a database seeder that dumps a statistics site. Reviewing my code, it no longer works, and I underestimate the reason a bit. $html->find()should return an array of elements found, however, it looks like it only finds the first table when used.

According to the documentation, I tried instead using find () and specifying each table id, however this also seems to be an error.

$table_passing = $html->find('table[id=passing]');

Can someone help me figure out what is wrong here? I don’t understand why none of these methods work, where the page source clearly shows several tables and identifiers, where both approaches should work.

private function getTeamStats()
{
    $url = 'http://www.pro-football-reference.com/years/2016/opp.htm';
    $html = file_get_html($url);

    $tables = $html->find('table');

    $table_defense = $tables[0];
    $table_passing = $tables[1];
    $table_rushing = $tables[2];

    //$table_passing = $html->find('table[id=passing]');

    $teams = array();

    # OVERALL DEFENSIVE STATISTICS #
    foreach ($table_defense->find('tr') as $row)
    {
        $stats = $row->find('td');

        // Ignore the lines that don't have ranks, these aren't teams
        if (isset($stats[0]) && !empty($stats[0]->plaintext))
        {
            $name = $stats[1]->plaintext;
            $rank = $stats[0]->plaintext;
            $games = $stats[2]->plaintext;
            $yards = $stats[4]->plaintext;

            // Calculate the Yards Allowed per Game by dividing Total / Games
            $tydag = $yards / $games;

            $teams[$name]['rank'] = $rank;
            $teams[$name]['games'] = $games;
            $teams[$name]['tydag'] = $tydag;
        }
    }

    # PASSING DEFENSIVE STATISTICS #
    foreach ($table_passing->find('tr') as $row)
    {
        $stats = $row->find('td');

        // Ignore the lines that don't have ranks, these aren't teams
        if (isset($stats[0]) && !empty($stats[0]->plaintext))
        {
            $name = $stats[1]->plaintext;
            $pass_rank = $stats[0]->plaintext;
            $pass_yards = $stats[14]->plaintext;

            $teams[$name]['pass_rank'] = $pass_rank;
            $teams[$name]['paydag'] = $pass_yards;
        }
    }

    # RUSHING DEFENSIVE STATISTICS #
    foreach ($table_rushing->find('tr') as $row)
    {
        $stats = $row->find('td');

        // Ignore the lines that don't have ranks, these aren't teams
        if (isset($stats[0]) && !empty($stats[0]->plaintext))
        {
            $name = $stats[1]->plaintext;
            $rush_rank = $stats[0]->plaintext;
            $rush_yards = $stats[7]->plaintext;

            $teams[$name]['rush_rank'] = $rush_rank;
            $teams[$name]['ruydag'] = $rush_yards;
        }
    }
+4
1

simplexml , XPath , , , @, -

$table_passing = $html->find('table[@id="passing"]');

DOMDocument DOMXPath, , "commented out" , html- : .

$url='http://www.pro-football-reference.com/years/2016/opp.htm';

$html=file_get_contents( $url );
/* remove the html comments */
$html=str_replace( array('<!--','-->'), '', $html );

libxml_use_internal_errors( true );
$dom=new DOMDocument;
$dom->validateOnParse=false;
$dom->standalone=true;
$dom->strictErrorChecking=false;
$dom->recover=true;
$dom->formatOutput=false;
$dom->loadHTML( $html );
libxml_clear_errors();  



$xp=new DOMXPath( $dom );
$tbl=$xp->query( '//table[@id="passing"]' );

foreach( $tbl as $n )echo $n->tagName.' > '.$n->getAttribute('id');

/* outputs */
table > passing
+1

Source: https://habr.com/ru/post/1655151/


All Articles