I am wondering how do I get information from a specific table of an HTML document? Right now, I am pulling all of the data from the entire page, but I only want the data from the table with an id of team_batting
. How would this be accomplished? Here is my code:
$dom = new DOMDocument();
$html = file_get_contents('https://www.baseball-reference.com/register/team.cgi?id=41270199');
@$dom->loadHTML($html);
$table = $dom->getElementByID('team_batting');
$stats = $dom->getElementsByTagName("td");
for ($i = 0; $i < $stats->length; $i++) {
$attr = $stats->item($i)->getAttribute('data-stat');
if ($attr != 'player') {
continue;
}
$nodes[] = $stats->item($i)->textContent;
foreach($nodes as $node) {
echo $node;
echo '<br>';
}
}
With this I am able to pull all of the player names from the data-stat="player"
attribute but I only want to apply this across the team_batting
table. How would I do this?