【发布时间】:2009-01-01 00:50:06
【问题描述】:
我正在尝试从网站中获取特定的原始文本。通过这个网站和其他资源,我学会了如何使用 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 中的文本?
谢谢!
【问题讨论】:
-
尝试使用 var_dump 而不是 print_r 来调查您的变量
标签: php screen-scraping simplexml