duannai5879 2013-12-01 16:02
浏览 50

使用Simple DOM Parser将Wordpress帖子提取到html

For parsing the strings inside my Wordpress posts to HTML. I use Simple DOM Parser. With the below code I receive Fatal error: Call to a member function find() on a non-object. Could somebody be of helping hand?

Thanks in advance

<?php
    require ('simple_html_dom.php');

    /*
    Template Name: Practice
    */


    $query = new WP_Query( 'category_name=arthouse' ); 
            if ($query->have_posts()) {
                    while ( $query->have_posts() ) {
                        $query->the_post();

                        $page = the_content().' ';
                        $html = str_get_html($page). " ";

                            foreach($html->find('div[class=post]') as $element) {
                                echo $element->src . '<br>';
                        }
                    }
                }else{
                    echo 'Sorry, no posts matched your criteria.';
                }

    ?>
  • 写回答

1条回答 默认 最新

  • doutan5844 2013-12-01 16:26
    关注

    First of all, I didn't understand, why you are using DOM parser here and if you are willing to get all the links from inside the div[class=post] using simple dom parser then you should write something like this

    foreach($html->find('div.post a') as $a){ // or div[class=post] a
        echo $a->href.'<br />'; // for link, i.e. http://somedomain.com/somepage.php
        // or
        echo $a; // to print out the link as it's
    }
    

    This may not right for your layout of html but it may give you an idea, also check this answer. If you try this code (fetch all the post title/link from my site), you may get an idea (scrapping my site is not allowed)

    $html = file_get_html('http://heera.it');
    foreach($html->find('section.post-body h3 a') as $a){
        echo $a.'<br />';
    }
    

    This (section.post-body h3 a) to get all the links from inside h3 tags which is inside section tag with class post-body. Again, make sure, why you are using a Dom parser ? I think, you don't need to use a parser, maybe you are doing it wrong, I think so.

    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥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 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看