I have a HTML which look like this, and I am trying to use simple HTML DOM to grab the Name value and Nickname value.
<tr>.....</tr>
<tr>.....</tr>
<tr>
<th>Name</th>
<td>John</td>
</tr>
<tr>
<th><span>Nickname</span></th>
<td>Johny</td>
</tr>
<tr>.....</tr>
<tr>.....</tr>
I am having difficulties as the th and td does not have id or classes
so i tried using innertext value with the following php code
require_once('lib/simple_html_dom.php');
$url='http://123.com';
$html = file_get_html($url);
foreach ( $html->find ( 'tr th' ) as $element ) {
if ($element->innertext = 'Name'){
$element = $element->parent;
$tinfo = $element->find ( 'td', 0 );
$info = $tinfo->plaintext;
echo $info;}
}
$html = file_get_html($url);
foreach ( $html->find ( 'tr th span' ) as $element ) {
if ($element->innertext = 'Nickname'){
$element = $element->parent;
$tinfo = $element->find ( 'td', 0 );
$info = $tinfo->plaintext;
echo $info;}
}
I could not seems to get the above code to work any advice on this? How do i accomplish this?
After removing some conflicting scripts, My new code, as advised by Ghost, now works for scraping name and nickname value
<?php
$name = $nickname ='';
foreach($html->find('tr') as $tr) { // each row
foreach($tr->childNodes() as $tdh) { // each cell of that row
if($tdh->tag == 'th' && $tdh->innertext == 'Name') {
$name = $tdh->next_sibling()->innertext;
}
}
}
echo $name;
foreach($html->find('tr th') as $tr) { // each row
foreach($tr->childNodes() as $tdh) { // each cell of that row
if($tdh->tag == 'span' && $tdh->innertext == 'Nickname') {
$nickname = $tdh->parent->next_sibling()->innertext;
}
}
}
echo $nickname;
?>
Credit to Ghost for the answer.