我正在使用 PHP 从网站获取数据,并尝试从该数据创建模型。这是我当前的代码:
$dom = new DOMDocument();
$html = file_get_contents('https://www.baseball-reference.com/register/team.cgi?id=41270199');
libxml_use_internal_errors(true);
$dom->loadHTML($html);
$table = $dom->getElementByID('team_batting');
$rows = $table->getElementsByTagName("tr");
for($i = 0; $i < $rows->length; $i++) {
$stats = $table->getElementsByTagName("td");
$name = $stats->item($i)->getAttribute('player');
$age = $stats->item($i)->getAttribute('age');
$plateAppearances = $stats->item($i)->getAttribute('PA');
$atBats = $stats->item($i)->getAttribute('AB');
$hits = $stats->item($i)->getAttribute('H');
$doubles = $stats->item($i)->getAttribute('2B');
$triples = $stats->item($i)->getAttribute('3B');
$homeruns = $stats->item($i)->getAttribute('HR');
$walks = $stats->item($i)->getAttribute('BB');
$strikeouts = $stats->item($i)->getAttribute('SO');
$name = $stats->item(0)->textContent;
$age = $stats->item(1)->textContent;
$plateAppearances = $stats->item(3)->textContent;
$atBats = $stats->item(4)->textContent;
$hits = $stats->item(6)->textContent;
$doubles = $stats->item(7)->textContent;
$triples = $stats->item(8)->textContent;
$homeruns = $stats->item(9)->textContent;
$walks = $stats->item(13)->textContent;
$strikeouts = $stats->item(14)->textContent;
$player = new Player([
'name' => $name,
'age' => $age,
'plateAppearances' => $plateAppearances,
'atBats' => $atBats,
'hits' => $hits,
'doubles' => $doubles,
'triples' => $triples,
'homeruns' => $homeruns,
'walks' => $walks,
'strikeouts' => $strikeouts
]);
echo $player;
echo '<br>';
}
这将检索我想要的所有属性,但只产生第一个玩家的 19 个实例(总行数),如下所示:
{"name":"Miguel Amaya","age":"19","plateAppearances":"241","atBats":"212","hits":"61","doubles":"14","triples":"2","homeruns":"9","walks":"24","strikeouts":"53"}
为了检索表中的所有玩家而不仅仅是第一个,我可以更改什么,并player
为每个人创建一个模型?
编辑/更新:添加了一些我从中提取数据的表
<tr ><th scope="row" class="right " data-stat="ranker" >1</th><td class="left " data-append-csv="player.fcgi?id=amaya-000mig" data-stat="player" csk="Amaya,Miguel" ><a href="/register/player.fcgi?id=amaya-000mig">Miguel Amaya</a></td><td class="right " data-stat="age" >19</td><td class="right " data-stat="G" >59</td><td class="right " data-stat="PA" >241</td><td class="right " data-stat="AB" >212</td><td class="right " data-stat="R" >29</td><td class="right " data-stat="H" >61</td><td class="right " data-stat="2B" >14</td><td class="right " data-stat="3B" >2</td><td class="right " data-stat="HR" >9</td><td class="right " data-stat="RBI" >33</td><td class="right " data-stat="SB" >0</td><td class="right " data-stat="CS" >0</td><td class="right " data-stat="BB" >24</td><td class="right " data-stat="SO" >53</td><td class="right " data-stat="batting_avg" >.288</td><td class="right " data-stat="onbase_perc" >.365</td><td class="right " data-stat="slugging_perc" >.500</td><td class="right " data-stat="onbase_plus_slugging" >.865</td><td class="right " data-stat="TB" >106</td><td class="right " data-stat="GIDP" >3</td><td class="right " data-stat="HBP" >3</td><td class="right " data-stat="SH" >0</td><td class="right " data-stat="SF" >2</td><td class="right " data-stat="IBB" >2</td><td class="right " data-stat="notes" ></td></tr>
问题是$stats
没有<td>
从循环的当前行获取元素。你将它设置为所有的<td>
表中的元素。改变
$stats = $table->getElementsByTagName("td");
至:
$stats = $rows[$i]->getElementsByTagName("td");
然后摆脱所有使用items($i)
. $i
是中的索引$rows
,与 无关$stats
。
此外,您需要跳过表中的标题行,它没有任何<td>
元素。使用它只获取 中的行<tbody>
,跳过<thead>
:
$rows = $table->getElementsByTagName("tbody")->item(0)->getElementsByTagName("tr");
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句