【发布时间】:2016-06-02 03:23:53
【问题描述】:
我想使用选择器提取网站某些部分中包含的内容。我正在使用简单的 HTML DOM 来执行此操作。但是由于某种原因,返回的数据多于我指定的选择器中的数据。我检查了FAQ of Simple HTML DOM,但没有看到任何可以帮助我的东西。我在 Stackoverflow 上也找不到任何东西。
我正在尝试获取此网页上 ul class="river" 中包含的所有 h2 class="hed" 标记的内容/href:@987654322 @
在我的输出中,我从其他标签(如 p class="dek has-dek")接收到大量数据,这些数据不包含在 h2 标签中,也不应该包含在内。这真的很奇怪,因为我认为代码只允许抓取这些标签中的内容。
如何将输出限制为仅包含 h2 标签中包含的数据?
这是我正在使用的代码:
<div class='rcorners1'>
<?php
include_once('simple_html_dom.php');
$target_url = "http://www.theatlantic.com/most-popular/";
$html = new simple_html_dom();
$html->load_file($target_url);
$posts = $html->find('ul[class=river]');
$limit = 10;
$limit = count($posts) < $limit ? count($posts) : $limit;
for($i=0; $i < $limit; $i++){
$post = $posts[$i];
$post->find('h2[class=hed]',0)->outertext = "";
echo strip_tags($post, '<p><a>');
}
?>
</div>
Output can be seen here。我不仅获得了几个文章链接,还获得了作者的信息、文章的信息等。
【问题讨论】:
标签: php web-crawler simple-html-dom