dongyan0629 2017-01-01 04:05
浏览 80
已采纳

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

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).

I inspected the element to gain it's XPath:

Getting XPath

Which gave me: //*[@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:

Code 1

But I just end up with empty elements in my table within my site:

enter image description here

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. "
";
                }
            }
        }

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).

enter image description here

Edit 2: So this is what I'm trying now:

$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;
                                    } 

enter image description here

  • 写回答

1条回答 默认 最新

  • dongshan4878 2017-01-01 05:29
    关注

    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. I suggest ignoring tbody and fetching only rows with //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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器