简体   繁体   English

HTML DOM分析器-如何在论坛中获得所有主题的第一篇文章

[英]HTML DOM Parser - How to get the first post of all topics in a forum

I was trying to scrap the first post of every topics in sitepoint javascript forum. 我正在尝试在sitepoint javascript论坛中剪贴每个主题的第一篇文章。 But The DOM Parser would give me ALL THE POSTS OF EVERY TOPICS IN SITE POINT JAVASCRIPT FORUM. 但是DOM解析器会在站点点Java脚本论坛中为我提供所有主题的所有帖子。 Maybe I didn't traverse the DOM correctly ? 也许我没有正确遍历DOM? Below is my code: 下面是我的代码:

<?php

class Sitepoint extends Controller
{
    public function index()
    {
        $this->load->helper('dom');
        header('Content-Type: text/html; charset=utf-8');
        echo '<ol>';

            $html = file_get_html('http://www.sitepoint.com/forums/javascript-15');

            foreach($html->find('a[id^="thread_title"]') as $topic) {
                $post =$topic->href;
                $posthtml = file_get_html($post);
                $posthtml->find('div[id^="post_message"]', 0);
                echo'<li>';
                echo $topic->plaintext.'<br>';
                echo $posthtml->plaintext.'<br>';
                echo'</li>';
            }
        echo '</ol>';
    }
}

You forgot to assign the result of $posthtml->find to a variable: 您忘记将$posthtml->find的结果分配给一个变量:

foreach($html->find('a[id^="thread_title"]') as $topic) {
    $post =$topic->href;
    $posthtml = file_get_html($post);
    $posttext = $posthtml->find('div[id^="post_message_"]', 0);
    echo'<li>';
    echo $topic->plaintext.'<br>';
    echo $posttext->plaintext.'<br>';
    echo'</li>';
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM