XPath在浏览器中工作,但在简单的HTML DOM中不起作用

时间:2015-05-16 21:50:53

标签: php xpath simple-html-dom

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

什么可以触发这个

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

它返回

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

1 个答案:

答案 0 :(得分:1)

您可以使用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");

并具有相同的效果。