1
嗨我想解析一个维基百科文档,其中有一个名为“信息框生物区”的表与这种结构。我试图得到以下特点如下表数据和类解析维基百科页表问题
王国:
界门:
亚门:
类:
订单:
家庭:
<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>
这里是我试图解析并获得具有表格结构的兔子的王国,门,亚门,阶级,家庭和秩序。然而,我得到了一个下面的Array([Kingdom:] => [门:] => [Subphylum:] => [Class:] => [Order:] => [Family:] => [
Pentalagus Bunolagus Nesolagus Romerolagus ] =>) 它没有填写与兔子的数据阵列。它也给我一个解析错误在下面显示的行,有什么可能是错的?
<?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);
?>
它的工作表示感谢!我需要更好地跟上这些PHP verisons – Squirtle