dongyue3795 2014-10-30 13:46
浏览 130
已采纳

通过DOMDocument PHP获取DIV元素内容

I have to recover some news from a div of a site. The div is structured as follows:

The HTML Markup:

<ul id="news-accordion" class="rounded" style="padding: 2px;">
   <li class="o">
         <h3>
            <span>TITLE ARTICLE</span>
            <span>30/10/2014</span>
         </h3>
         <div style="display: none;">
              <p>text of article</p>
         </div>
   </li>
   <li class="e">
         <h3>
            <span>TITLE ARTICLE</span>
            <span>28/10/2014</span>
         </h3>
         <div style="display: none;">
              <p>text of article</p>
         </div>
   </li>
   <li class="o">
         <h3>
            <span>TITLE ARTICLE</span>
            <span>29/10/2014</span>
         </h3>
         <div style="display: none;">
              <p>text of article</p>
         </div>
   </li>                                                     
</ul>

PHP

<?php 

$doc = new DomDocument;
$doc->validateOnParse = true;
$doc->loadHtml(file_get_contents('http://www.xxxxxxxxx/news.php'));

$news = $doc->getElementById('news-accordion');

$li = $news->getElementsByTagName('li'); 

foreach ($li as $row){ 

    $title = $row->getElementsByTagName('h3'); 
    echo $title->item(0)->nodeValue."<br><br>"; 

    /*foreach ($title as $row2){ 
    echo $row2->nodeValue."<br><br>";
    //echo $row2->item(0)->nodeValue."<br><br>"; 
    }*/

    $text = $row->getElementsByTagName('p'); 
    echo utf8_decode($text->item(0)->nodeValue)."<br><br><br>"; 

}

?>

The code works correctly, but when I print the contents of the span tag echo $title->item(0)->nodeValue;,

The text of the two span is printed together.

How can I take the contents of the two span separately? Thanks.

  • 写回答

2条回答 默认 最新

  • dongxu4580 2014-10-30 13:59
    关注

    Yes you can, just adjust the ->item() index. Just like what you have done already in the other elements, point it to that header element, then just explicitly point it to those span children:

    foreach ($li as $row){ 
    
        $h3 = $row->getElementsByTagName('h3')->item(0);
        $title = $h3->getElementsByTagName('span')->item(0); // first span
        $date = $h3->getElementsByTagName('span')->item(1); // second span
    
        echo $title->nodeValue . '<br/>';
        echo $date->nodeValue . '<br/>';
    
        $text = $row->getElementsByTagName('p'); 
        echo utf8_decode($text->item(0)->nodeValue)."<br><br><br>"; 
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?