【发布时间】:2014-08-27 01:18:23
【问题描述】:
我有一个 PHP 代码可以使用“子标题”类名从该网站检索类别。但是,输出什么也不显示。我究竟做错了什么?
PHP 代码:
<?php
header('Content-Type: text/html; charset=utf-8');
$grep = new DoMDocument();
@$grep>loadHTMLFile("http://www.alibaba.com/Products",false,stream_context_create(array("http" => array("user_agent" => "any"))));
$finder = new DomXPath($grep);
$class = "sub-title";
$nodes = $finder->query("//*[contains(@class, '$class')]");
foreach ($nodes as $node) {
$span = $node->childNodes;
echo $span->item(0)->nodeValue;
}
?>
期望的输出:
农业
食品和饮料
服装
等等。
谢谢!
【问题讨论】:
-
@Ghost 谢谢!很好的帮助
标签: php html xpath web-scraping domdocument