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