Search code examples
phphtmlsimple-html-dom

Simple HTML Dom Can't access 3rd level DIV element


I am trying to display the value 2.11 by using the attribute of data-gamename="1x2" from the third DIV, The code below are from original source.

<div class="games">==$0 
  <div class="wrap"> 
<div class="outcome" onclick="addBet(this)" id="outcome-225162629" data-gameid="61158022" data-outcomeid="225162629" data-gamename="1X2" data-eventname="Benevento Calcio - Carpi FC" data-eventcodeid="137" data-gamename="1X2">2.11</div>

From the above code if i want retrieve values of it will work good and if i want to retrieve values from it will work good too and make to make a record this second div is inside the first div of there it come another one inside the second div of it is called My problem start here if i want to retrieve data from lets say it will work like charm but if i change the attribute lets say like in example above it will not work, it just display nothing i tried to solve with no lucky , As the code look above can any one help me where am wrong?

This is the code for retrieve the value

<?php 
include('simple_html_dom.php'); 
$html = file_get_html('yoururl'); 
foreach($html->find('div=[data-gamename="1X2"]') as $a){
echo $a->plaintext; 
echo nl2br('\n'); 
} 
?>

Any idea where am wrong?


Solution

  • I see 3 problems, first the css is wrong, there's an extra = in there. Second, simple_html_dom is old and doesn't fully support css. For a library that does better I suggest this one. Third, why do you call your div $a? That's confusing.

    include('advanced_html_dom.php'); 
    $html = file_get_html('https://sport.premierbet.com/tz/sport/1/#'); 
    foreach($html->find('div[data-gamename="1X2"]') as $div){
      echo $div->plaintext; 
      echo nl2br('\n'); 
    }