PHP:使用xpath()从html页面获取内容 [英] PHP: Fetch content from a html page using xpath()

查看:452
本文介绍了PHP:使用xpath()从html页面获取内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用xpath和domdocument在html页面中获取div的内容。这是页面的结构:

I'm trying to fetch the content of a div in a html page using xpath and domdocument. This is the structure of the page:

<div id="content">
<div class="div1"></div>
<span class="span1></span>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<div class="div2"></div>
</div>

我只想获取p的内容,而不是span and divs。我通过这个xpath表达式 .//* [@ id ='content'] / p ,但由于我只得到了第一个p,所以猜测有些不对。尝试使用其他表达式

I want to get only the content of p, not spans and divs. I came thru this xpath expression .//*[@id='content']/p but guess something's not right because i'm getting only the first p. Tried using other expression with following-sibling and node() but all return the first p only.

.//*[@id='content']/span/following-sibling::p
.//*[@id='content']/node()[self::p]

这是xpath的用法:

This is how's used xpath:

$domDocument=new DOMDocument();
$domDocument->encoding = 'UFT8';
$domDocument->loadHTML($page);
$domXPath = new DOMXPath($domDocument);
$domNodeList = $domXPath->query($this->xpath);
$content = $this->GetHTMLFromDom($domNodeList);

这就是我从节点获取html的方式:

And this is how i get html from nodes:

private function GetHTMLFromDom($domNodeList){
$domDocument = new DOMDocument();
$node = $domNodeList->item(0);   
 foreach($node->childNodes as $childNode)
 $domDocument->appendChild($domDocument->importNode($childNode, true));
return $domDocument->saveHTML();
}


推荐答案

此XPath表达式:

//div[@id='content']/p

所需节点集中的结果(五个 p 元素)

Result in the wanted node set (five p elements)

编辑:现在很清楚您的问题是什么。您需要遍历NodeList:

EDIT: Now it's clear what is your problem. You need to iterate over the NodeList:

private function GetHTMLFromDom($domNodeList){ 
   $domDocument = new DOMDocument(); 
   foreach ($nodelist as $node) {
      $domDocument->appendChild($domDocument->importNode($node, true)); 
   }
   return $domDocument->saveHTML(); 
} 

这篇关于PHP:使用xpath()从html页面获取内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆