HTML DOM 解析器 - 如何获取论坛中所有主题的第一篇文章
我试图废弃 sitepoint javascript 论坛中每个主题的第一篇文章。但是 DOM 解析器会给我 SITE POINT JAVASCRIPT 论坛中每个主题的所有帖子。也许我没有正确遍历 DOM?下面是我的代码:
<?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>';
}
}
I was trying to scrap the first post of every topics in sitepoint javascript forum. But The DOM Parser would give me ALL THE POSTS OF EVERY TOPICS IN SITE POINT JAVASCRIPT FORUM. Maybe I didn't traverse the DOM correctly ? 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>';
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您忘记将
$posthtml->find
的结果分配给变量:You forgot to assign the result of
$posthtml->find
to a variable: