简体   繁体   English

尝试的XPath查询未显示任何结果

[英]Attempted XPath query not showing any results

I'm currently working on a fantasy sports site, and I want to be able to pull basic stats from another site. 我目前在幻想体育网站上工作,并且希望能够从其他网站获取基本统计信息。 (I don't have much experience with XML or pulling data from other sites). (我对XML或从其他站点提取数据没有太多经验)。

I inspected the element to gain it's XPath: 我检查了该元素以获得XPath:

获取XPath

Which gave me: //*[@id="cp1_ctl01_pnlPlayerStats"]/table[1]/tbody/tr[4]/td[18] 这给了我: //*[@id="cp1_ctl01_pnlPlayerStats"]/table[1]/tbody/tr[4]/td[18]

I've looked into a couple methods of trying to pull the info and came up with this: 我研究了几种尝试获取信息的方法,并提出了以下方法:

代码1

But I just end up with empty elements in my table within my site: 但是我最终只是在网站内的表中添加了空元素:

在此处输入图片说明

Here's My Code: 这是我的代码:

        $doc = new DOMDocument();
        @$doc->loadHTMLFile($P_RotoLink);

        $xpath = new DOMXpath($doc);

        $elements = $xpath->query('//*  [@id="cp1_ctl01_pnlPlayerStats"]/table[1]/tbody/tr[4]/td[18]');

        if (!is_null($elements)) {
            foreach ($elements as $element) {
                $nodes = $element->childNodes;
                foreach ($nodes as $node) {
                    echo $node->nodeValue. "\n";
                }
            }
        }

A few things I've tried have thrown me errors, and any time I finally get pass them or suppress them I get empty content. 我尝试过的一些事情给我抛出了错误,每当我终于通过它们或压制它们时,我都会得到空的内容。 I've tried a bunch of different formats but none seem to give me the desired content. 我尝试了很多不同的格式,但似乎都没有给我想要的内容。

Edit: Here's the source HTML, I want to grab the value within the td (13.0). 编辑:这是源HTML,我想在td(13.0)内获取值。

在此处输入图片说明

Edit 2: So this is what I'm trying now: 编辑2:这就是我现在正在尝试的方法:

$html = file_get_contents($P_RotoLink);

$doc = new DOMDocument;
libxml_use_internal_errors(true);
$doc->loadHTML($html);
libxml_use_internal_errors(false);
$xpath = new DOMXpath( $doc);

foreach ($xpath->query('//*[@id="cp1_ctl01_pnlPlayerStats"]/table//tr[4]/td[18]') as $node) {
                                        $ppg = substr($node->textContent,0,3);
                                        echo $ppg;
                                    } 

在此处输入图片说明

The problem is that the table in the screenshot doesn't have tbody node, but your XPath expression includes tbody which causes DOMXPath::query to return an empty list of nodes. 问题在于屏幕快照中的表没有tbody节点,但是您的XPath表达式包含tbody ,这导致DOMXPath::query返回空的节点列表。 I suggest ignoring tbody and fetching only rows with //tr . 我建议忽略tbody并仅使用//tr获取行。

Example

$html = <<<'HTML'
<div id="cp1_ctl01_pnlPlayerStats">
  <table>
    <tr></tr>
    <tr>
      <td><span>0.9</span>1.0<span>3.0</span></td><td>2.0</td>
    </tr>
  </table>
</div>
HTML;

$doc = new DOMDocument();
$doc->loadHTML($html);
$xp = new DOMXPath($doc);
$expr = '//*[@id="cp1_ctl01_pnlPlayerStats"]/table//tr[2]/td[1]/text()';
$td = $xp->query($expr);
if ($td->length) {
  var_dump($td[0]->nodeValue);
}

Output 输出量

string(3) "1.0"

The text() function selects all text node children of the context node. text()函数选择上下文节点的所有文本节点子级。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM