I'm trying to parse a website to provide information to my site... I'm able to get the individual cell values, but I'm having problems getting any of the tag information when the cell contains an image... specifically, I'm wanting either the
"alt" or "title" attribute values.. below is a snippet of the table i'm trying to parse, and my code...
I'm also having a problem where if I'm looping the process more than 2-3 times, the server gives me a 500 error... any ideas how i can make it more efficient capturing the html, so the server side doesnt time out?
Thanks for the help!
<table id="defaultTable" width="100%" cellpadding="0" cellspacing="0">
<tr class='row1'>
<td width="13"> </td>
<td height="35"><a href="/Server/Name/">Name</a></td>
<td height="35" class="fieldValue"> Guild </td>
<td height="35" class="fieldValue" align="center"> ## </td>
<td height="35" class="fieldValue" align="center"><img alt="Race" title="Race" src="/race.gif" vspace="5"></td>
<td height="35" class="fieldValue" align="center"><img alt="Class (##)" title="Class (##)" src="/class.gif" vspace="5"></td>
<td height="35" class="fieldValue" align="center"> <img alt="Trade (##)" title="Trade (##)" src="/trade.gif" vspace="5"></td>
<td height="35" class="fieldValue"><a href="/Server/" class="fieldValue">Server</a></td>
<td width="13"> </td>
</tr>
$file = "http://URL/" . $name . "/";
$doc = new domDocument;
if ($doc->loadHTMLFile($file)) {
$tables = $doc->getElementByID('defaultTable');
if ($tables) {
$rows = $tables->getElementsByTagName('tr');
/*** loop over the table rows ***/
foreach ($rows as $row) {
$cols = $row->getElementsByTagName('td');
$i = 0;
foreach ($cols as $cell) {
switch ($i) {
case 0 :
echo 'Name: ' . $cell->nodeValue.'<br />';
break;
case 1 :
echo 'Guild: ' . $cell->nodeValue.'<br />';
break;
case 2 :
echo 'Level: ' . $cell->nodeValue.'<br />';
break;
case 3 :
echo 'Race: ' . $cell->getElementsByTagName('img')->getNamedItem("alt")->nodeValue . '<br />';
break;
case 6 :
echo 'Server: ' . $cell->nodeValue.'<br />';
break;
}
$i++;
}
}}}