Skip to content
Advertisement

Parsing Wikipedia Page tables issue

Hi I’m trying to parse a Wikipedia document in which there is a table called “infobox biota” with this structure. I’m trying to get the following table data and classes of the following characteristics

Kingdom:
Phylum:
Subphylum:
Class:
Order:
Family:

<table class="infobox biota" style="text-align: left; width: 200px; font-size: 100%">
<tbody><tr>
<th colspan="2" style="text-align: center; background-color: rgb(211,211,164)">Rabbit</th>
</tr>
<tr>
<td colspan="2" style="text-align: center"><a href="/wiki/File:Rabbit_in_montana.jpg" class="image"><img alt="" src="//upload.wikimedia.org/wikipedia/commons/thumb/3/3b/Rabbit_in_montana.jpg/250px-Rabbit_in_montana.jpg" srcset="//upload.wikimedia.org/wikipedia/commons/thumb/3/3b/Rabbit_in_montana.jpg/375px-Rabbit_in_montana.jpg 1.5x, //upload.wikimedia.org/wikipedia/commons/thumb/3/3b/Rabbit_in_montana.jpg/500px-Rabbit_in_montana.jpg 2x" height="222" width="250"></a></td>
</tr>
<tr>
<th colspan="2" style="text-align: center; background-color: rgb(211,211,164)"><a href="/wiki/Biological_classification" title="Biological classification">Scientific classification</a></th>
</tr>
<tr>
<td>Kingdom:</td>
<td><span class="kingdom" style="white-space:nowrap;"><a href="/wiki/Animal" title="Animal">Animalia</a></span></td>
</tr>
<tr>
<td>Phylum:</td>
<td><span class="phylum" style="white-space:nowrap;"><a href="/wiki/Chordate" title="Chordate">Chordata</a></span></td>
</tr>
<tr>
<td>Subphylum:</td>
<td><span class="subphylum" style="white-space:nowrap;"><a href="/wiki/Vertebrata" title="Vertebrata" class="mw-redirect">Vertebrata</a></span></td>
</tr>
<tr>
<td>Class:</td>
<td><span class="class" style="white-space:nowrap;"><a href="/wiki/Mammal" title="Mammal">Mammalia</a></span></td>
</tr>
<tr>
<td>Order:</td>
<td><span class="order" style="white-space:nowrap;"><a href="/wiki/Lagomorpha" title="Lagomorpha">Lagomorpha</a></span></td>
</tr>
<tr>
<td>Family:</td>
<td><span class="family" style="white-space:nowrap;"><a href="/wiki/Leporidae" title="Leporidae">Leporidae</a><br>
<small>in part</small></span></td>
</tr>
<tr>
<th colspan="2" style="text-align: center; background-color: rgb(211,211,164)">Genera</th>
</tr>
<tr>
<td colspan="2" style="text-align: left">
<div>
<table style="background-color:transparent;table-layout:fixed;" border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody><tr valign="top">
<td>
<div style="margin-right:20px;">
<p><i><a href="/wiki/Pentalagus" title="Pentalagus" class="mw-redirect">Pentalagus</a></i><br>
<i><a href="/wiki/Bunolagus" title="Bunolagus" class="mw-redirect">Bunolagus</a></i><br>
<i><a href="/wiki/Nesolagus" title="Nesolagus">Nesolagus</a></i><br>
<i><a href="/wiki/Romerolagus" title="Romerolagus" class="mw-redirect">Romerolagus</a></i></p>
</div>
</td>
<td>
<div style="margin-right: 20px;">
<p><i><a href="/wiki/Brachylagus" title="Brachylagus" class="mw-redirect">Brachylagus</a></i><br>
<i><a href="/wiki/Sylvilagus" title="Sylvilagus" class="mw-redirect">Sylvilagus</a></i><br>
<i><a href="/wiki/European_Rabbit" title="European Rabbit" class="mw-redirect">Oryctolagus</a></i><br>
<i><a href="/wiki/Poelagus" title="Poelagus" class="mw-redirect">Poelagus</a></i></p>
</div>
</td>
</tr>
</tbody></table>
</div>
</td>
</tr>
</tbody></table>

Here is my attempt to parse and obtain the kingdom,phylum,subphylum,class,order and family of a rabbit with the table structure. However I get a the following Array ( [Kingdom:] => [Phylum:] => [Subphylum:] => [Class:] => [Order:] => [Family:] => [

Pentalagus Bunolagus Nesolagus Romerolagus ] => ) it doesnt fill in the array with the data for the rabbit. also it give me a parse error in the line shown below, what can be wrong?

<?php
//require"mydb.php";
header('Content-type: text/html; charset=utf-8'); // this just makes sure encoding is right
include('simple_html_dom.php'); // the parser library

$html = file_get_html('http://en.wikipedia.org/wiki/Rabbit');
$table = $html->find('table.infobox');

$data = array();

foreach($table[0]->find('tr') as $row)
{    
    $td = $row->find('> td');

    if (count($td) == 2)
    {
        $name = $td[0]->innertext;
        $text = $td[1]->find('a')[0]->innertext;   //PARSE ERROR IS GIVEN HERE, after the find('a')[0], taking off the array takes away the error but just me no results

        $data[$name] = $text;
    }
}

print_r($data);
?>

Advertisement

Answer

$text = $td[1]->find('a')[0]->innertext; 

In this line you are dereferencing a function. This is only available in PHP 5.4 or later. Try this instead:

$td = $td[1]->find('a');
$text = $td[0]->innertext;
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement