使用Php简单html dom解析器获取2篇文章
我正在尝试使用下面的代码从我的博客中获取一些帖子,但这有点困难使用Php简单html dom解析器获取2篇文章,php,simple-html-dom,Php,Simple Html Dom,我正在尝试使用下面的代码从我的博客中获取一些帖子,但这有点困难 include('simple_html_dom.php'); $html->clear(); getArticles('http://www.example.com'); function getArticles($page) { global $articles, $descriptions; $html = new simple_html_dom(); $html->load_file(
include('simple_html_dom.php');
$html->clear();
getArticles('http://www.example.com');
function getArticles($page) {
global $articles, $descriptions;
$html = new simple_html_dom();
$html->load_file($page);
$items = $html->find('li');
foreach($items as $post) {
# remember comments count as nodes
$articles[] = array($post->children(1)->innertext);
}
我的html结构是:
<li class="post-1840 post type-post status-publish format-standard hentry category-perierga">
<a href="http://www.example.com/?p=1840" title="my post title">
<img width="200" height="179" src="images/iko-200x179.jpg" class="attachment-archive wp-post-image" alt="iko-200x179.jpg" title=""></a>
<h2 class="leading"><a href="http://www.example.com/?p=1840">My Post Title!</a></h2>
<p class="widgetmeta sserif">
6 hours ago |
<a href="http://www.example.com/?cat=2" title="View all posts in Weird" rel="category">Weird</a> |
<a href="http://www.example.com/?author=1" title="Posts by adminadmin" rel="author">admin</a> |
<span>Comments Off</span>
</p>
<p class="teaser">my shord post description...</p>
<a class="mainbutton fr" href="http://www.example.com/?p=1840">Read More »</a>
</li>
我的文章描述
我想获取img(即y post title),它包含我的帖子的简短描述
再次感谢你们,伙计们 如果我理解得很好,那么很容易获得所有这些信息。。。以下是方法:
// includes Simple HTML DOM Parser
include "simple_html_dom.php";
// The input
$text = '<li class="post-1840 post type-post status-publish format-standard hentry category-perierga">
<a href="http://www.example.com/?p=1840" title="my post title">
<img width="200" height="179" src="images/iko-200x179.jpg" class="attachment-archive wp-post-image" alt="iko-200x179.jpg" title=""></a>
<h2 class="leading"><a href="http://www.example.com/?p=1840">My Post Title!</a></h2>
<p class="widgetmeta sserif">
6 hours ago |
<a href="http://www.example.com/?cat=2" title="View all posts in Weird" rel="category">Weird</a> |
<a href="http://www.example.com/?author=1" title="Posts by adminadmin" rel="author">admin</a> |
<span>Comments Off</span>
</p>
<p class="teaser">my shord post description...</p>
<a class="mainbutton fr" href="http://www.example.com/?p=1840">Read More »</a>
</li>';
//Create a DOM object
$html = new simple_html_dom();
// Load HTML from a string
$html->load($text);
// Find all li elements
$items = $html->find('li');
// loop into each li element
foreach($items as $i => $post) {
// get the img
$img = $post->find('img', 0)->src;
// get the post's url
$url = $post->find('a', 0)->href;
// get the title
$title = $post->find('a', 0)->title;
// another way to get the title
$title2 = $post->find('h2.leading', 0)->plaintext;
// get the description
$desc = $post->find('p.teaser', 0)->plaintext;
// Print all
echo "\n$i => $img | $url | $title | $title2 | $desc";
echo "<hr/>";
}
// Clear dom object
$html->clear();
unset($html);
OUTPUT:
=======
0 => images/iko-200x179.jpg | http://www.example.com/?p=1840 | my post title | My Post Title! | my shord post description...
//包括简单的HTML DOM解析器
包括“simple_html_dom.php”;
//输入
$text='
我的文章描述
';
//创建DOM对象
$html=新的简单html\U dom();
//从字符串加载HTML
$html->load($text);
//查找所有li元素
$items=$html->find('li');
//循环到每个li元素中
foreach($i=>$post形式的项目){
//获取img
$img=$post->find('img',0)->src;
//获取帖子的url
$url=$post->find('a',0)->href;
//得名
$title=$post->find('a',0)->title;
//另一种获得头衔的方式
$title2=$post->find('h2.leading',0)->纯文本;
//得到描述
$desc=$post->find('p.trister',0)->纯文本;
//全部打印
echo“\n$i=>$img |$url |$title |$title2 |$desc”;
回声“
”;
}
//清除dom对象
$html->clear();
未结算($html);
输出:
=======
0=>images/iko-200x179.jpg|http://www.example.com/?p=1840 |我的帖子标题|我的帖子标题!|我的帖子描述。。。
hmm,这是什么意思
我想获取img,即(y post title)
?你到底想要什么?我没有得到它,我需要从我的wordpress博客上获取一些帖子。我试图从rss2获取它,但rss2中没有图像。我需要显示的图像,链接,标题和简短的描述。