【发布时间】:2015-06-14 15:27:54
【问题描述】:
我正在尝试使用 XPath 获取必应搜索结果。这是我的代码:
$html = file_get_contents("http://www.bing.com/search?q=bacon&first=11");
$doc = new DOMDocument();
libxml_use_internal_errors(true);
$doc->loadHtml($html);
$x = new DOMXpath($doc);
$output = array();
// just grab the urls for now
foreach ($x->query("//li[@class='b_algo']") as $node)
{
//$output[] = $node->getAttribute("href");
$tmpDom = new DOMDocument();
$tmpDom->loadHTML($node);
$tmpDP = new DOMXPath($tmpDom);
echo $tmpDP->query("//div[@class='b_title']//h2//a//href");
}
return $output;
这个foreach遍历所有结果,我要做的就是从foreach中的$node中提取链接和文本,但是因为$node本身是一个对象,我无法从中创建DOMDocument它。怎么查询?
【问题讨论】:
-
为什么不
//li[@class='b_algo']//div[@class='b_title']//h2//a//href? -
试试上面提到的@splash58。必须工作。
-
你没有在这里使用 XQuery,PHP 甚至不支持它。请不要发送不相关的标签。