John Baker
John Baker

Reputation: 435

Getting data from HTML using DOMDocument

I'm trying to get data from HTML using DOM. I can get some data, but can't figure out how to get the rest. Here is an image highlighting the data I want.

https://i.sstatic.net/7WmwS.png

here is the code itself

http://pastebin.com/Re8qEivv

and here my PHP code

$html = file_get_contents('result.html');
$dom = new DOMDocument;
$dom->loadHTML($html);
$tr = $dom->getElementsByTagName('tr');

foreach ($tr as $row){

    $td = $row->getElementsByTagName('td');
    $td1 = $td->item(1);
    $td2 = $td->item(2);

    foreach ($td1->childNodes as $node){
        $title = $node->textContent;      
    }

    foreach ($td2->childNodes as $node){
        $type = $node->textContent;
    }

}

Upvotes: 0

Views: 523

Answers (2)

John Baker
John Baker

Reputation: 435

Figured it out

$html = file_get_contents('result.html');
$dom = new DOMDocument;
$dom->loadHTML($html);
$tr = $dom->getElementsByTagName('tr');

foreach ($tr as $row){

    $td = $row->getElementsByTagName('td');
    $td1 = $td->item(1);
    $td2 = $td->item(2);

    $title = $td1->childNodes->item(0)->textContent;
    $firstURL = $td1->getElementsByTagName('a')->item(0)->getAttribute('href');

    $type = $td2->childNodes->item(0)->textContent;
    $imageURL = $td2->getElementsByTagName('img')->item(0)->getAttribute('src');  

}

Upvotes: 2

Manish Shukla
Manish Shukla

Reputation: 1365

I have used following class. http://sourceforge.net/projects/simplehtmldom/

This is very simple and easy to use class.

You can use

$html->find('#RosterReport > tbody', 0);

to find specific table

$html->find('tr')
$html->find('td')

to find table rows or columns

Note $html is variable have full html dom content.

Upvotes: 0

Related Questions