Jump to content

fetching DIV that are inside unordered list and list item with simple html dom


ruckser

Recommended Posts

How can i get DIV that are inside ul and li like the below codes what shows, there is alot of DIVinside a single li what method should i use? there is codes i have used below but they fetch all liincluding those i don't need, need your thought how can i obtain example <div class="single-event__code"> from <li class="single-event live"> see below php and css codes

 

<div class="app_content">

<ul class="js-events-container">...</li>
<li class="single-event live">...</li>
<li class="single-event live">...</li>
<li class="single-event live">...</li>
<li class="single-event live">...</li>
<li class="single-event live">
::before
//First DIV of li

<div class="single-event__code">
Event Code: <strong>96441</strong>
</div>
<div class="single-event__time">
<span class="score js-score">LIVE</span>
<span class="time js-timer"></span>
</div>
<div class="single-event__players">
<a href="/sportsbook/SOCCER/BRAZIL_SERIE_D/400953/">
AA Portuguesa RJ <span> v </span> Audax SP </a>
</div>
<div class="single-event__bets three markets">
<div class="single-event__bets__container js-bets-container">
<div class="single-event__bets__price">
1.02
</div><div class="single-event__bets__price">
81.00
</div><div class="single-event__bets__price">
101.00
</div></div>
<div class="single-event__bets__more"><a href="/sportsbook/SOCCER/BRAZIL_SERIE_D/400953/" data-markets="+18">+<span>+18</span></a></div>
</div>
::after
</li>

<li class="single-event live">...</li>
<li class="single-event live">...</li>
<li class="single-event live">...</li>
<li class="single-event live">...</li>

 

PHP CODES

 

<?php

include("advanced_html_dom.php");
$html = file_get_html("https://www.mkekabet.com/sportsbook/SOCCER/");

foreach($html->find("div.app__content ul li") as $a){
echo $a->plaintext;
echo nl2br("\n");
}
$html->clear();

?>

Link to comment
Share on other sites

Use DOMDocument and an XPath query to grab the elements you want.

 

Something like this:

<?php

$html = file_get_contents('http://...');
$dom = new DOMDocument();
$dom->loadHTML($html);
$xpath = new DOMXPath($dom);

$codeList = $xpath->query('//div[@class="single-event__code"]');
foreach ($codeList as $codeDiv){
    var_dump($codeDiv->textContent);
}

Side-note: In the future, be sure to wrap your posted code inside
  • Like 1
Link to comment
Share on other sites

Use DOMDocument and an XPath query to grab the elements you want.

 

Something like this:

<?php

$html = file_get_contents('http://...');
$dom = new DOMDocument();
$dom->loadHTML($html);
$xpath = new DOMXPath($dom);

$codeList = $xpath->query('//div[@class="single-event__code"]');
foreach ($codeList as $codeDiv){
    var_dump($codeDiv->textContent);
}

Side-note: In the future, be sure to wrap your posted code inside

tags as I did above, it makes the code more readable and preserves spacing.

 

@kicken I got you bro, so i tried the code and here is how they react, If i want scrap a div that is not inside a unordered list/list item it will be scraped successfully but if i want to scrap a div that is between a list item like

<div class="single-event__code"></div>

it will not be printed out, Any idea?? i tried to go through the link you gave me above but i have found nothing. Below are the codes.

<div class="app_content">
<ul class="js-events-container">...</li>
<li class="single-event live">...</li>
<li class="single-event live">...</li>
<li class="single-event live">...</li>
<li class="single-event live">...</li>
<li class="single-event live">
::before
//First DIV of li
<div class="single-event__code">
Event Code: <strong>96441</strong>
</div>
<div class="single-event__time">
<span class="score js-score">LIVE</span>
<span class="time js-timer"></span>
</div>
<div class="single-event__players">
<a href="/sportsbook/SOCCER/BRAZIL_SERIE_D/400953/">
AA Portuguesa RJ <span> v </span> Audax SP </a>
</div>
<div class="single-event__bets three markets">
<div class="single-event__bets__container js-bets-container">
<div class="single-event__bets__price">
1.02
</div><div class="single-event__bets__price">
81.00
</div><div class="single-event__bets__price">
101.00
</div></div>
<div class="single-event__bets__more"><a href="/sportsbook/SOCCER/BRAZIL_SERIE_D/400953/" data-markets="+18">+<span>+18</span></a></div>
</div>
::after
</li>

<li class="single-event live">...</li>
<li class="single-event live">...</li>
<li class="single-event live">...</li>
<li class="single-event live">...</li>

PHP Code

<?php
 
$html = file_get_contents('https://www.mkekabet.com/sportsbook/SOCCER/');
$dom = new DOMDocument();
$internalErrors = libxml_use_internal_errors(true);
$dom->loadHTML($html);
libxml_use_internal_errors($internalErrors);
$xpath = new DOMXPath($dom);
 
$codeList = $xpath->query('//div[@class="app__content"]');
foreach ($codeList as $codeDiv){
    var_dump($codeDiv->textContent);
}
 
Edited by ruckser
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.