Question

I'm using the Simple HTML DOM Parser - http://simplehtmldom.sourceforge.net/manual.htm I'm trying to scrape some data from a scoreboard page. The below example shows me pulling the HTML of the "Akron Rushing" table.

Inside $tr->find('td', 0), the first column, there is a hyperlink. How can I extract this hyperlink? Using $tr->find('td', 0')->find('a') does not seem to work.

Also: I can write conditions for each table (passing, rushing, receiving, etc), but is there a more efficient way to do this? I'm open to ideas on this one.

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 />";
        }
   }
}
Was it helpful?

Solution

In your case, the find('tr') returns 10 elments instead of the 7 rows expected only.

Also, not all the names has links associated with them, trying to retrieve a link when it doesnt exist may return an error.

Therefore, here's a modified working version of your code:

$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 />";
            }

        }
   }
}

As you can see, an attribute can be reched using this format $tag->attributeName. In your case, attributeName is href

Notes:

It would be a good idea to handle find's errors, knowing that it returns "False" when nothing is found

$td = $tr->find('td');

// Find suceeded
if ($td) {
    // code here
}
else
  echo "Find() failed in XXXXX";

PHP Simple HTML DOM Parser has known memory leaks issues with php5, so don't forget to free up memory when DOM objects are no more used:

$html = file_get_html(...);

// do something... 

$html->clear(); 
unset($html);

Source: http://simplehtmldom.sourceforge.net/manual_faq.htm#memory_leak

OTHER TIPS

According to the documentation you should be able to chain selectors for nested elements.

This is the example they give:

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

The only difference I can see is that they include the index in the second find. Try added that in and seeing if it works for you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top