我正在尝试从网站上获取特定的原始文本。通过使用这个站点和其他资源,我学会了如何使用 simpleXML 和 xpath 获取特定图像。
然而,相同的方法似乎不适用于抓取原始文本。这是现在不起作用的东西。
// first I set the xpath of the div that contains the text I want
$xpath = '//*[@id="storyCommentCountNumber"]';
// then I create a new DOM Document
$html = new DOMDocument();
// then I fetch the file and parse it (@ suppresses warnings).
@$html->loadHTMLFile($url);
// then convert DOM to SimpleXML
$xml = simplexml_import_dom($html);
// run an XPath query on the div I want using the previously set xpath
$commcount = $xml->xpath($xpath);
print_r($commcount);
现在,当我抓取图像时,该 commcount 对象将返回一个数组,其中包含其中某处的图像源。
在这种情况下,我希望该对象返回包含在“storyCommentCountNumber”div 中的原始文本。但该文本似乎不包含在对象中,只是 Div 的名称。
我究竟做错了什么?我可以看到这种方法仅用于抓取 HTML 元素及其内部的位,而不是原始文本。如何获取该 div 中的文本?
谢谢!