Php Dom文档结果错误


Php Dom Document results error

我想从html中抓取一些元素,但我无法根据需要抓取数据。

html

<div class="opinions">
<ul>
<li>
<div class="imgcontainers">
<a href="domainname.com" title="title">                                                 `<img width="160" src="image.jpg" />`
</a>
</div>
<p class="caption">
<a href="domainname.com" class="head">asdfad</a>
<span>November 03, 2015 09:29 This is article title</span>
</p>
</li>
</ul>
</div>
$dom = new DOMDocument();
$classname = 'opinions';
$html = get_page($url);
@$dom->loadHTML($html);
$dom->preserveWhiteSpace = false;
$xpath = new DOMXPath($dom);
$articles = $xpath->query("//*[@class='" . $classname . "']");
$p = $articles->getElementsByTagName('a');
$div = $articles->getElementsByTagName('div');
foreach($p as $value){
    $title = $value->getAttribute("href");
    echo $title;
}

当我运行这个脚本时,我得到了这个错误"调用未定义的方法DOMNodeList::getElementsByTagName()"

我真正需要的是,我需要每个href链接和img src路径(如果有的话)以及它的span文本值。请就如何实现这一目标提出建议。

也许你可以从我的代码中学到一些东西

或者,如果你决定包含我的功能,下面是我的操作方法:

    $html = ""; //your html
    $props = array(
    array("tagname"=>"div", "props"=>array("class"=>"opinions")),
    //the '/' before 'a' is for all descendant <a> of <div>
    array("tagname"=>"/a"),
    );
    $options = array("property"=>"href");
    require_once 'getNodeValue.php';
    $hrefs = getNodeValue($html, $props, $options);
    print_r($hrefs);