我试图使用simple_html_dom废弃一些内容而没有运气...... 我试图抓住标题,图像路径和链接并显示它。
html结构如下:
<div class="article_item clearfix">
<h2 class="title"><a href="http://www.demodomain/articleid=1">My amazing Title</a></h2>
<p class="date">September 22 2014</p>
<p class="image_left">
<a href="http://www.demodomain/articleid=1">
<img src="http://www.demodomain/photos/cef78533cd5.jpg" alt="My amazing post ">
</a>
</p>
<p>This is a demo description<strong>of this amazing</strong> article</p>
<p class="more"><a href="http://www.demodomain/articleid=1" class="blued_links">Read more...</a></p>
</div>
到目前为止我的代码:
foreach($html->find('article_item') as $article) {
$item['title'] = $article->find('.title, a', 0)->plaintext;
$item['thumb'] = $article->find('.image_left img', 0)->src;
$item['details'] = $article->find('p', 0)->plaintext;
$item['url'] = $article->find('.more, a', 0)->plaintext;
echo 'Title: ' . $item['title'];
echo "</br>";
echo "image url: " . $item['thumb'];
echo "</br>";
echo "Description: " . $item['details'];
echo "</br>";
echo "Read More Url: " . $item['url'];
}
// Clear dom object
$html->clear();
unset($html);
答案 0 :(得分:1)
你没有说明什么不起作用,但考虑这个例子:
foreach($html->find('div.article_item') as $div) {
// ^ point to div tag with class name article_item
$title = $div->find('h2.title a ', 0)->innertext;
// ^ target the h2 tag with class title with child anchor
// just same as accessing dom with jquery
$thumb = $div->find('p.image_left img ', 0)->src;
$details = $div->children(3)->plaintext;
// $url = $div->find('p.more', 0)->plaintext;
$url = $div->find('p.more a', 0)->href;
echo $title . '<br/>';
echo $thumb . '<br/>';
echo $details . '<br/>';
echo $url . '<br/>';
}
基本上,这与选择选择器相同。
答案 1 :(得分:0)
$item['title'] = $article->find('h2.title')->plaintext;
$item['thumb'] = $article->find('p.image_left')->find('img')->src;