我正在使用Simple HTML DOM从网站获取元素,但是当class属性有空格时,我什么也得不到。
<table id="table-type-2" class="stats-table stats-main table-2">
<tbody>
<tr class="odd glib-participant-ppjDR086" data-def-order="0">
<td class="rank col_rank no" title="">1.</td>
<td class="participant_name col_participant_name col_name"><span class="team_name_span"><a onclick="javascript:getUrlByWinType('/soccer/england/premier-league/teaminfo.php?team_id=ppjDR086');">Manchester United</a></span></td>
<td class="matches_played col_matches_played">4</td>
<td class="wins col_wins">4</td>
<td class="draws col_draws">0</td>
<td class="losses col_losses">0</td>
<td class="goals col_goals">14:0</td>
<td class="goals col_goals">12</td>
</tr>
<tr class="even glib-participant-hA1Zm19f" data-def-order="1">
<td class="rank col_rank no" title="">2.</td>
<td class="participant_name col_participant_name col_name"><span class="team_name_span"><a onclick="javascript:getUrlByWinType('/soccer/england/premier-league/teaminfo.php?team_id=hA1Zm19f');">Arsenal</a></span></td>
<td class="matches_played col_matches_played">4</td>
<td class="wins col_wins">4</td>
<td class="draws col_draws">0</td>
<td class="losses col_losses">0</td>
<td class="goals col_goals">11:3</td>
<td class="goals col_goals">12</td>
</tr>
<tr class="odd glib-participant-Wtn9Stg0" data-def-order="2">
<td class="rank col_rank no" title="">3.</td>
<td class="participant_name col_participant_name col_name"><span class="team_name_span"><a onclick="javascript:getUrlByWinType('/soccer/england/premier-league/teaminfo.php?team_id=Wtn9Stg0');">Manchester City</a></span></td>
<td class="matches_played col_matches_played">4</td>
<td class="wins col_wins">3</td>
<td class="draws col_draws">1</td>
<td class="losses col_losses">0</td>
<td class="goals col_goals">18:3</td>
<td class="goals col_goals">10</td>
</tr>
</tbody>
</table>
<?php
include('../simple_html_dom.php');
function getHTML($url,$timeout)
{
$ch = curl_init($url); // initialize curl with given url
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]); // set useragent
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // write the response to a variable
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // follow redirects if any
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); // max. seconds to execute
curl_setopt($ch, CURLOPT_FAILONERROR, 1); // stop when it encounters an error
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
return @curl_exec($ch);
}
$response=getHTML("http://www.betexplorer.com/soccer/england/premier-league/standings/?table=table&table_sub=home&ts=WOO1nDO2&dcheck=0",10);
$html = str_get_html($response);
$team = $html->find("span[class=team_name_span]/a");
$numbermatch = $html->find("td.matches_played.col_matches_played");
$wins = $html->find("td.wins.col_wins");
$draws = $html->find("td.draws.col_draws");
$losses = $html->find("td.losses.col_losses");
$goals = $html->find("td.goals.col_goals");
?>
<table border="1" width="100%">
<thead>
<tr>
<th>Team</th>
<th>MP</th>
<th>W</th>
<th>D</th>
<th>L</th>
<th>G</th>
</tr>
</thead>
<?php
foreach ($team as $match) {
echo "<tr>".
"<td class='first-cell'>".$match->innertext."</td> " .
"<td class='first-cell'>".$numbermatch->innertext."</td> " .
"<td class='first-cell'>".$wins->innertext."</td> " .
"<td class='first-cell'>".$draws->innertext."</td> " .
"<td class='first-cell'>".$losses->innertext."</td> " .
"<td class='first-cell'>".$goals->innertext."</td> " .
"</tr><br/>";
}
?>
</table>
所以,我只得到第一个值(因为类名没有空格),但是我不能得到其余的值
编辑:我修错了PHP代码。再看一遍
EDIT2:这不是重复,我尝试了解决方案,但它不起作用
EDIT3:我尝试使用advanced_html_dom(它应该修复空间问题),但我没有得到任何东西(也是我唯一得到的东西)
EDIT4:在下面的屏幕中,您可以看到我想要获得的内容以及我现在得到的内容:
EDIT5
team.php
<?php
// START team.php
class Team
{
public $name, $matches, $wins, $draws, $losses, $goals;
public static function parseRow($row): ?self
{
$result = new self();
$result->name = $result->parseMatch($row, 'span.team_name_span a');
if (null === $result->name) {
return null; // couldn't even match the name, probably not a team row, skip it
}
$result->matches = $result->parseMatch($row, 'td.col_matches_played');
$result->wins = $result->parseMatch($row, 'td.col_wins');
$result->draws = $result->parseMatch($row, 'td.col_draws');
$result->losses = $result->parseMatch($row, 'td.col_losses');
$result->goals = $result->parseMatch($row, 'td.col_goals');
return $result;
}
private function parseMatch($row, $selector)
{
if (!empty($match = $row->find($selector, 0))) {
return $match->innertext;
}
return null;
}
}
// END team.php
?>
clas.php
<?php
include('../simple_html_dom.php');
include('../team.php');
function getHTML($url,$timeout)
{
$ch = curl_init($url); // initialize curl with given url
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]); // set useragent
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // write the response to a variable
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // follow redirects if any
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); // max. seconds to execute
curl_setopt($ch, CURLOPT_FAILONERROR, 1); // stop when it encounters an error
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
return @curl_exec($ch);
}
$response=getHTML("http://www.betexplorer.com/soccer/england/premier-league/standings/?table=table&table_sub=home&ts=WOO1nDO2&dcheck=0",10);
$html = str_get_html($response);
// START DOM parsing block
$teams = [];
foreach($html->find('table.stats-table tr') as $row) {
$team = Team::parseRow($row); // load the row into a Team object if possible
// skipp this entry if it couldn't match the row
if (null !== $team) {
// what were actually doing here is just the OOP equivalent of:
// $teams[] = ['name' => $row->find('span.team_name_span a',0)->innertext, ...];
$teams[] = $team;
}
}
foreach($teams as $team) {
echo $team->name;
echo $team->matches;
}
// END DOM Parsing Block
?>
答案 0 :(得分:1)
解决方案: http://phpfiddle.org/main/code/cq54-hta2
SimpleHtmlDom不支持这样的属性选择器。另外,您可以使用类来匹配类,就好像它在类名中有空格一样。所以,而不是:
$wins = $html->find("td[class=wins col_wins]");
$draws = $html->find("td[class=draws col_draws]");
$losses = $html->find("td[class=losses col_losses]");
执行以下操作以匹配与两个类名称匹配的td元素:
$wins = $html->find("td.wins.col_wins");
$draws = $html->find("td.draws.col_draws");
$losses = $html->find("td.losses.col_losses");
此外,如果您只是这样做,HTML标记并不要求您匹配这两个类来获取数据:
$wins = $html->find("td.col_wins");
$draws = $html->find("td.col_draws");
$losses = $html->find("td.col_losses");
您要提取的是来自表行的数据数组。更具体地说,看起来像这样:
$teams = [
['Arsenal', matches, wins, ...],
['Liverpool', matches, wins, ...],
...
];
这意味着您需要针对表的每一行运行相同的数据提取。 SimpleHtmlDom通过类似jQuery的find
方法简化了这一过程,可以从任何匹配的元素中调用它们。
此解决方案实际上定义了一个Team
对象来加载每行的数据。应该使未来的调整更加简单。
这里要注意的重要一点是,首先我们将每个表格行循环为$row
,然后从$row->find([selector])
收集团队和数字。
// START team.php
class Team
{
public $name, $matches, $wins, $draws, $losses, $goals;
public function __construct($row)
{
$this->name = $this->parseMatch($row, 'span.team_name_span a');
if (null === $this->name) {
return; // couldn't even match the name, probably not a team row, skip it
}
$this->matches = $this->parseMatch($row, 'td.col_matches_played');
$this->wins = $this->parseMatch($row, 'td.col_wins');
$this->draws = $this->parseMatch($row, 'td.col_draws');
$this->losses = $this->parseMatch($row, 'td.col_losses');
$this->goals = $this->parseMatch($row, 'td.col_goals');
}
private function parseMatch($row, $selector)
{
if (!empty($match = $row->find($selector, 0))) {
return $match->innertext;
}
return null;
}
public function isValid()
{
return null !== $this->name;
}
public function getMatchData() //example
{
return "<br><b>". $this->wins .' : '. $this->matches . "</b>";
}
}
// END team.php
// START DOM parsing block
$teams = [];
foreach($html->find('table.stats-table tr') as $row) {
$team = new Team($row); // load the row into a Team object if possible
// skipp this entry if it couldn't match the row
if ($team->isValid()) {
// what were actually doing here is just the OOP equivalent of:
// $teams[] = ['name' => $row->find('span.team_name_span a',0)->innertext, ...];
$teams[] = $team;
}
}
foreach($teams as $team) {
echo "<h1>".$team->name."</h1>";
echo $team->losses;
echo $team->getMatchData();
}
// END DOM Parsing Block