XPath 在浏览器中有效,但在简单 HTML DOM 中无效

XPath works in browser, but not in simple HTML DOM

我正在尝试解析包含特定字符串的特定对象。使用我在 XPath 下面创建的 XPathHelper,它在网站上运行良好,但是当我尝试以编程方式进行时。它 returns 什么都没有?

什么会触发这个

include('simple_html_dom.php');
$gosu_full = file_get_html("http://www.gosugamers.net/counterstrike/news/30998-  cph-wolves-disbands-cs-go-team");

echo $gosu_full->find("//em[contains(text(), 'More content on GosuGamers:')]", 0);

当我这样做的时候

echo $gosu_full->find("//em", 0);

它returns

<em>More content on GosuGamers:</em>

您可以使用 PHP DOM 而不是 simple_html_dom.php,如下所示:

$doc = new DOMDocument();
// loadHTMLFile generates many warnings, so we want to suppress them
@$doc->loadHTMLFile("http://www.gosugamers.net/counterstrike/news/30998-  cph-wolves-disbands-cs-go-team");

$xpath = new DOMXpath($doc);
$elements = $xpath->query("//em[contains(text(), 'More content on GosuGamers:')]");

echo $elements->item(0)->nodeValue;

但是,您在这里根本不需要 [contains(text(), 'More content on GosuGamers:')],因此您可以将其减少为:

$elements = $xpath->query("//em");

效果相同