如何从DOMElement php抓取兄弟DOMNode?

时间:2015-03-15 01:44:36

标签: php web-scraping domdocument

我正在使用PHP,我正在尝试删除一小段代码,但在这一刻我不知道如何。以下是我不能废弃的简单结构,双引号内的文字。

<strong>Palabras: </strong>
<br>            
"   
     Biometría,          
     Análisis de textura,                    
     Wavelets,        
     Codificación predictiva,  
     Reconocimiento de patrones,                      
     Filtros Bidimensionales de Gabor, 
"                   
<br>

原始文字为here

Producción bibliográfica - Artículo - Publicado en revista especializada
some name,another name, "E-Learning y Espacios Colaborativos" . En: CountryName
ISSN: ed:
v. fasc. p. - ,2006 
Palabras: 
E-learning, Espacios Colaborativos, 
Sectores: 
Educación,

这是我试图废除双引号内的文字

 //getting Palabras text content
  $list = $doc->getElementsByTagName('strong');
  foreach($list as $node)
  {
      if( $node->nodeValue == "Palabras: " )
      {
         //what can I do here to get the double quotations content
      }
  }

如果比较为真$node->nodeValue == "Palabras: "我尝试使用“兄弟”节点获取内容,如下所示:

if( $node->nodeValue == "Palabras: " )
{
    $nodeValue = $node->nextSibling->nodeValue;
}

但是如果我尝试这样做,我会收到一个错误,其中问题是$node->nextSiblingDOMElement,因此$node->nextSibling不属于nodeValue {1}}。

那么如何才能获得“兄弟”DOMNode

注意:

为什么我不打电话给$doc->getElementsByTagName('br')代替$doc->getElementsByTagName('strong'),因为网页上有很多br个标签,但我只需要<strong>Palabras: </strong>之后的文字(这是唯一标识双引号内文本内容的标签,我没有计划在它们之间找到br标签

1 个答案:

答案 0 :(得分:2)

您可以使用XPath表达式查找<strong>Palabras: </strong>,然后查找第一个不完全由空格组成的兄弟文本节点。


实施例

$xpath = new DOMXPath($doc);
$query = '//strong[.="Palabras: "]/following-sibling::text()[normalize-space()][1]';

foreach ($xpath->query($query) as $node) {
    echo $node->nodeValue;
}