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];
$teams = array();
foreach ($table_defense->find('tr') as $row)
{
$stats = $row->find('td');
if (isset($stats[0]) && !empty($stats[0]->plaintext))
{
$name = $stats[1]->plaintext;
$rank = $stats[0]->plaintext;
$games = $stats[2]->plaintext;
$yards = $stats[4]->plaintext;
$tydag = $yards / $games;
$teams[$name]['rank'] = $rank;
$teams[$name]['games'] = $games;
$teams[$name]['tydag'] = $tydag;
}
}
foreach ($table_passing->find('tr') as $row)
{
$stats = $row->find('td');
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;
}
}
foreach ($table_rushing->find('tr') as $row)
{
$stats = $row->find('td');
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;
}
}
user470760