Johnny Bravo
Johnny Bravo

Reputation: 3

Issue with HTML Simple dom PHP scraper

I'm trying to parse the Team Statistics from this website.

I want to parse "Key stats" block, here is the screenshot

Wins / draws / losses - 363 / 8 / 168
Total kills - 50715
Total deaths - 45101
Rounds played - 14083
K/D Ratio - 1.12
Best player(Average rating) - olofmeister (1.15)

Screenshot (I have no rep, to add image, sorry)

I'm using Simple HTML Dom PHP parser and I've started with the basic thing. I've extracted all links, for a testing purpose and it worked fine for me.

include 'simple_html_dom.php';

$url = 'http://www.hltv.org/?pageid=179&teamid=4991&gameid=2';
$html = file_get_html($url);

foreach($html->find('a') as $element) {
    echo $element->href . '<br>';
}

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

Afterwards, I've started extracting the main div block, where everything is kept:

include 'simple_html_dom.php';

$url = 'http://www.hltv.org/?pageid=179&teamid=4991&gameid=2';
$html = file_get_html($url);

foreach ($html->find('div[style="float:right;width:300px;"]') as $div) {
    echo $div . '<br/>';
};

It was working fine and results were satisfying - prntscr.com/88p8l1

Then, I've started to getting deeper and got stuck.

include 'simple_html_dom.php';

$url = 'http://www.hltv.org/?pageid=179&teamid=4991&gameid=2';
$html = file_get_html($url);

foreach ($html->find('div[style="float:right;width:300px;"]') as $div) {
    $item['stat-title'] =  $html->find('div[style="height:22px;background-color:white"]')->plaintext;
    $item['stat-data']  =  $html->find('div[style="height:22px;background-color:white"]')->plaintext;
    $items[] = $item;
};

print_r($items);

At this point, I really struggle, how to display the results I need.

I've tested separately one part of the code - and it was working fine.

foreach ($html->find('div[style="height:22px;background-color:#E6E5E5"]') as $div) {
    echo $div . '<br/>';
};

The result I want achieve:

<div class="stat">
    <span class="stat-title">Wins / draws / losses</span>
    <span class="stat-data">363 / 8 / 168</span>
</div>

I need a fresh view on my current problem. Thank you in advance.

Upvotes: 0

Views: 125

Answers (1)

Holybreath
Holybreath

Reputation: 413

$item;

foreach ($html->find('div.covGroupBoxContent div.covSmallHeadline') as $div) {
        if(isset($div->style) && $div->style=="font-weight:normal;width:180px;float:left;color:black;text-align:right;") {
            //select black text which is the stat data
            $item["stat-data"] = $div->plaintext;
            //the previous sibling of the data is the title (based on the website)
            $item["stat-title"] = $div->prev_sibling()->plaintext;
            $items[] = item;
        }
    };

Hope this helps. Please define the question properly.

Upvotes: 1

Related Questions