dongyan3018 2014-11-07 18:58
浏览 85

Simple_html_dom获取标题和介绍并在我的页面上显示它们

As I want to understand Simple HTML Dom a bit I am playing around with it, to test options on my localhost.

Basically I want to take the titles and intro's of this website and display them on my page.

The title as <h2> and the intro as <p>.

What am I doing wrong?

<?php
include 'simple_html_dom.php';
// Create DOM from URL
$html = file_get_html('http://www.nu.nl/algemeen');

foreach($html->find('div[class=list-overlay]') as $article){
    $title['intro']    = $article->find('span[class=title]', 0)->innertext;
    $intro['details'] = $article->find('span[class=excerpt]', 0)->innertext;


    echo '<h2>'. $articles . '</h2>
    <p>'. $title .'</p>';
}
?>

edit: There was a double line in there.

  • 写回答

1条回答 默认 最新

  • dongxieyi9115 2014-11-07 21:30
    关注

    Your soulution is somehow right. You have only few typos in variable names. Here is my editation of your code. Also I have added few comments to help you understand.

    <?php
        include 'simple_html_dom.php';
        // Create DOM from URL
        $html = file_get_html('http://www.nu.nl/algemeen');
    
        // exctract all elements matching selector div[class=...]
        foreach($html->find('div[class=list-overlay]') as $article){
            // and for each extract first (0) element that matches to span[class=title]
            $title = $article->find('span[class=title]',   0)->innertext;
            // and do the same for intro, extract first element that belongs to selector
            $intro = $article->find('span[class=excerpt]', 0)->innertext;
    
            // and write it down simply
            echo '<h2>'. $title . '</h2>';
            echo '<p>' . $intro . '</p>';
        }
    ?>
    

    This solution isn't good though. The have bad structure of their HTML so it is not easy to select only articles, because they don't have them in div with ID articles (for example. You are lucky man anyway, because they provide you XML feed of their articles that is much easier to parse (also less data to transfer and so on). You can find it here and extract titles and intros for your website.

    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么