php xpath 评估重复数据只获取第一行 [英] php xpath evaluate duplicate data get only first row

查看:23
本文介绍了php xpath 评估重复数据只获取第一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的 PHP 代码:

This is my PHP code:

<?php

error_reporting(E_ALL);
ini_set("display_errors",1);

ini_set('max_execution_time', 36000); //300 seconds = 5 minutes

$url = 'http://www.sportstats.com/soccer/matches/20170815/';

libxml_use_internal_errors(true); 
$doc = new DOMDocument();
$doc->loadHTMLFile($url);
$xpath = new DOMXpath($doc);


$data = array(

'HomeTeam' => $xpath->evaluate('string(//td[@class="table-home"]/a)'),
'AwayTeam' => $xpath->evaluate('string(//td[contains(@class, "table-away")]/a)'),
'FtScore' => $xpath->evaluate('string(normalize-space(translate(//td[@class="result-neutral"]," " ,"")))'),
'HomeTeamid' => $xpath->evaluate('substring-before(substring-after(substring-after(//td[@class="table-home"]/a/@href, "/soccer/"),"-"),"/")'),
'AwayTeamid' => $xpath->evaluate('substring-before(substring-after(substring-after(//td[@class="table-away"]/a/@href, "/soccer/"),"-"),"/")')

);

foreach ($data as $key) {

echo $data['HomeTeamid'].",";
echo $data['HomeTeam'].",";
echo $data['FtScore'].",";
echo $data['AwayTeam'].",";
echo $data['AwayTeamid']."<br/>";

}

?>

但是脚本给出了重复的结果:

But the script gives duplicate results:

n3QdnjFB,Santos,0-0,Fluminense,EV9L3kU4
n3QdnjFB,Santos,0-0,Fluminense,EV9L3kU4
n3QdnjFB,Santos,0-0,Fluminense,EV9L3kU4
n3QdnjFB,Santos,0-0,Fluminense,EV9L3kU4
n3QdnjFB,Santos,0-0,Fluminense,EV9L3kU4

但我希望它看起来像...

But I want it's look like...

 HTeamid,Santos,0-0,Fluminense,ATeamid
 HTeamid,Cartagena,1-0,Llaneros,ATeamid
 HTeamid,Cerro Porteno,1-1,Libertad Asuncion,ATeamid
 HTeamid,Operario,2-1,Maranhao,ATeamid
 HTeamid,Emelec,2-0,Fuerza,ATeamid
 ...
 ..
 .

匹配列表图片我查看了网站上的其他问题并没有找到答案我如何使用 echo 命令获取所有其他团队的数据(我不想使用 var_dump).谢谢.

Matches list image I looked at other questions on the site and didn"t find an answer How do i do get all other teams data with echo command (I don't want to do with var_dump). Thanks.

推荐答案

这里有两个错误,你在位置路径中使用了//td.这使得相对于文档的路径和字符串函数始终返回列表中第一个节点的文本内容.你总是第一个游戏.

Here are two mistakes, you use //td in the location path. This makes the path relative to the document and the string function always return the text content of the first node in the list. You get always the first game.

获取列表数据的典型结构是:

The typical structure for fetching list data is:

foreach($xpath->evaluate($exprForItems) as $item) {
  $detail = $xpath->evaluate($exprForDetail, $item);
}

一个更具体的例子:

$document = new DOMDocument();
$document->loadHtml($html);
$xpath = new DOMXpath($document);

$expressions = new stdClass();
// this is the expression for items - it returns a node list
$expressions->games = '//div[@id = "LS_todayMatchesContent"]/table/tbody/tr';
// this are detail expressions - they return a string
$expressions->home = 'string(td[@class = "table-home"]/a)';
$expressions->homeId = 'substring-before(substring-after(substring-after(td[@class="table-home"]/a/@href, "/soccer/"),"-"),"/")';
$expressions->away= 'string(td[@class = "table-away"]/a)';

foreach ($xpath->evaluate($expressions->games) as $game) {
  var_dump(
    [
      $xpath->evaluate($expressions->home, $game),
      $xpath->evaluate($expressions->homeId, $game),
      $xpath->evaluate($expressions->away, $game)
    ]
  );
}

输出:

array(3) {
  [0]=>
  string(6) "Santos"
  [1]=>
  string(8) "n3QdnjFB"
  [2]=>
  string(10) "Fluminense"
}
array(3) {
  [0]=>
  string(9) "Cartagena"
  [1]=>
  string(8) "6eofBSjQ"
  [2]=>
  string(8) "Llaneros"
}
//...

所以只有detail 表达式使用字符串函数,并且它们总是需要it​​em 节点作为上下文(第二个参数).你必须小心使用上下文.

So only the detail expressions use the string functions and they always need the item node as the context (second argument). You have to be careful to use the context.

这篇关于php xpath 评估重复数据只获取第一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆