通过XPath提取HTML字段

时间:2012-12-17 14:02:36

标签: php dom xpath simplexml

我有这个查询提取已被“喜欢”超过5次的帖子。

//div[@class="pin"]
[.//span[@class = "LikesCount"]
[substring-before(normalize-space(text())," ") > 5]

我想提取和存储其他信息,例如title,img url,number,repin number,......

如何全部提取它们?

  • 多个XPath查询?
  • 在使用php和php函数进行迭代时深入研究结果帖子的节点?
  • ...

遵循标记示例:

<div class="pin">

<p class="description">gorgeous couch <a href="#">#modern</a></p>

[...]

<div class="PinHolder">
<a href="/pin/56787645270909880/" class="PinImage ImgLink">
    <img src="http://media-cache-ec3.pinterest.com/upload/56787645270909880_d7AaHYHA_b.jpg" 
         alt="Krizia" 
         data-componenttype="MODAL_PIN" 
         class="PinImageImg" 
         style="height: 288px;">
</a>
</div>

<p class="stats colorless">
    <span class="LikesCount"> 
        22 likes 
    </span>
    <span class="RepinsCount">
        6 repins
    </span>
</p>

[...]

</div>

1 个答案:

答案 0 :(得分:2)

由于您已在代码中使用XPath,我建议您也使用XPath提取该信息。这里有一个关于如何提取描述的例子。

<?php 

// will store the posts as assoc arrays
$mostLikedPostsArr = array();

// call your fictional load function
$doc = load_html('whatever');

// create a XPath selector
$selector = new DOMXPath($doc);

// this your query from above
$query = '//div[@class="pin"][.//span[@class = "LikesCount"][substring-before(normalize-space(text())," ") > 5]';

// getting the most liked posts
$mostLikedPosts = $selector->query($query);

// now iterate through the post nodes
foreach($mostLikedPosts as $post) {

    // assoc array for a post
    $postArr = array();

    // you can do 'relative' queries once having a reference to $post
    // note $post as the second parameter to $selector->query()

    // lets extract the description for example
    $result = $selector->query('p[@class = "description"]', $post);
    // just using nodeValue might be ok for text only nodes.
    // to properly flatten the <a> tags inside the descriptions 
    // it will take further attention.
    $postArr['description'] = $result->item(0)->nodeValue;

    // ...

    $mostLikedPostsArr []= $postArr;
}