获取所有具有itemprop属性的元素 [英] Get all elements that have an attribute of itemprop

查看:210
本文介绍了获取所有具有itemprop属性的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用PHP DomDocument并尝试抓取如下所示的内容:

I'm using PHP DomDocument and trying to scrape out something that looks like this:

<div itemprop='movie'>Fight Club</div>

它也可能看起来像这样:

it could also look like this:

<span itemprop='musician'>Ozzy Osbourne</span>

我想获取所有 itemprop ='n'在页面上,然后将它们放入数组中以存储其nodevalue和关联的itemprop名称。到目前为止,我的代码如下:

I want to grab all of the itemprop='n' on the page and put them into an array to store their nodevalue, and the associated itemprop name. My code so far looks like this:

function getItemprops(){
        foreach($this->dom->getAttribute("itemprop") as $buffer) {
                $itempropList = array(
                    'theNodeValue' => $buffer->nodeValue,
                    'theItemprop'  => $buffer->getAttribute("itemprop")
                )
                return $itempropList;
        }
}

我的代码应该在以下行:

My code is supposed to get an array somewhere along the lines of:

array (
      array(
         0 =>
              "theNodeValue" => "Fight Club",
              "theItemprop"  => "movie"
         1 =>
              "theNodeValue" => "Fight Club",
              "theItemprop"  => "movie"
      )
)

不幸的是,我的代码返回致命错误:调用未定义的方法DOMDocument :: getAttribute()

Unfortunately, my code returns Fatal error: Call to undefined method DOMDocument::getAttribute().

所以基本上,我想选择所有 itemprop = 并将它们放入数组。

So basically, I want to select all itemprop=""'s and put them in arrays.

感谢所有帮助!

推荐答案

您需要使用XPath首先选择具有所需属性的所有节点,然后循环遍历返回的节点以获取文本值和属性值;像这样

You need to use XPath to select all nodes with your required attribute first, then loop trhough the returned nodes to get text value and attribute value; like this

$d = new DOMDocument();
$d->loadHTML($xmlsource);
$xpath = new DOMXPath($d);
$nodes = $xpath->query('//*[@itemprop]');  //this catches all elements with itemprop attribute
foreach ($nodes as $node) { 
   // do your stuff here with $node

这篇关于获取所有具有itemprop属性的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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