I have html code like this:
<td class="table-main__odds" data-oid="4ci66xv464x0xbdci3" data-odd="2.18"></td>
<td class="table-main__odds colored" data-oid="4ci66xv498x0x0">
<span>
<span>
<span data-odd="3.68"></span>
</span>
</span>
</td>
<td class="table-main__odds" data-oid="4ci66xv464x0xbdci4" data-odd="3.09"></td>
<td class="table-main__odds" data-oid="4ci60xv464x0xbdchn" data-odd="10.35"></td>
<td class="table-main__odds" data-oid="4ci60xv498x0x0" data-odd="6.12"></td>
<td class="table-main__odds colored" data-oid="4ci60xv464x0xbdcho">
<span>
<span>
<span data-odd="1.26"></span>
</span>
</span>
</td>
I need to get data-odd values, but you can see some values are into td tag, some values are into span tag, but all are data-odd
I'm trying this approach:
<?php
include_once('../simple_html_dom.php');
$url = "xyz";
function curl_request($url, $timeout = 30) {
// Initialize curl with given url
$ch = curl_init($url);
// Set user-agent
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]);
// Write the response to a variable
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Follow redirects
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
// Max seconds to execute
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
// Stop on error
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
return curl_exec($ch);
}
function get_html($url) {
return str_get_html(curl_request($url));
}
$html = get_html($url);
$b = 0;
$search = $html->find('td[class=table-main__odds], td[class=table-main__odds colored] span');
foreach($search as $allOdds){
$quote = array($allOdds->href, $allOdds->innertext);
if (isset($allOdds->attr['data-odd'])) {
$quote['data-odd'] = $allOdds->attr['data-odd'];
}
$quotes[] = $quote;
}
foreach($quotes as $mark) {
echo $mark[0]. " ";
}
?>
but I got the follow error: Fatal error: Call to a member function find() on a non-object in... on line 33 (foreach line)
Any suggestion?
Thanks
EDIT: I put $html = get_html($url);
EDIT2: I added var_dump($quotes); after foreach cicle
EDIT3: My output is like this: see new image
If you simply want to get the attribute values of the data-odd
attribute in your html, try something simple like this:
$doc = new DOMDocument();
$doc->loadHTML($html);
$xp = new DOMXPath($doc);
$odds = $xp->query('//*[@data-odd]/@data-odd');
foreach ($odds as $odd) {
echo($odd->value) . "\r\n";
}
Output:
2.18
3.68
3.09
10.35
6.12
1.26
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.