CURL:按类名获取元素

时间:2014-09-21 14:35:43

标签: php dom curl

以下是我通过DOM获得的内容(我删除了内容):

    <ul class="toggle-block">
        <li>
            <h4 class="chip ui-header rounded-5 act-select toggle-active" ></h4>
            <div class="inner rounded-5 act-block">
                <div class="inlineBlock"></div>
                <div class="inlineBlock"></div>
                <div></div>
                <h5></h5>
                <div class="inlineBlock"></div>
                <div class="inlineBlock"></div>
            </div>
        </li>
    </ul>

我需要从 act-block 主div中获取每个 div h5 中的每个内容。

我已经开始使用此代码了:

$xpath = new DOMXPath($dom);
$els = $xpath->query('//ul[@class="toggle-block"]/li/div');

但现在我不知道该怎么做。有什么想法吗?

编辑1:

我已经对此进行了测试,它正在为第一个节点工作......

$xpath = new DOMXPath($dom);
$actblock = $xpath->query("//div[contains(concat(' ',normalize-space(@class),' '),' act-block ')]");
$els = $xpath->query('./div | ./h5', $actblock->item(0));
$el = $els->item(0)->nodeValue;
echo $el;

......但它不能用于以下方面:

$xpath = new DOMXPath($dom);
$actblock = $xpath->query("//div[contains(concat(' ',normalize-space(@class),' '),' act-block ')]");
$els = $xpath->query('./div | ./h5', $actblock->item(0));
$el = $els->item(1)->nodeValue;
echo $el;

1 个答案:

答案 0 :(得分:0)

此查询应该有效:

$actblock = $xpath->query("//div[contains(concat(' ',normalize-space(@class),' '),' act-block ')]");
$els = $xpath->query('./div | ./h5', $actblock->item(0));

请参阅:http://pivotallabs.com/xpath-css-class-matching/Xpath select multiple tags