解析使用PHP简单的HTML DOM解析器

问题描述:

我试图分析此网站(获得IMG链接)从RSS饲料IMG:http://statigr.am/feed/parishilton解析使用PHP简单的HTML DOM解析器

这是我的代码:

include 'parse/simple_html_dom.php'; 

// Create DOM from URL or file 
$html = file_get_html('http://statigr.am/feed/parishilton/'); 

// Find all images 
foreach($html->find('img') as $element) 
{ 
     echo $element->src . '<br>'; 
}  

脚本不返回任何东西!这是为什么 ?我想要img链接。

这是因为所有的图像都在里面CDATA部分和分析器忽略它,因此该解决方案是

$html = file_get_html('http://statigr.am/feed/parishilton/'); 
$html = str_replace("<![CDATA[","",$html); // clean-up 
$html = str_replace("]]>","",$html); // clean-up 
$html = str_get_html($html); // re-construct the dom object 
// Loop 
foreach($html->find('item description img') as $el) 
{ 
    echo $el->src . "<br />"; 
} 

从返回的内容替换所有CDATA然后用str_get_html创建从该字符串遍历DOM对象图片。 (测试和工作)。

输出:

http://distilleryimage3.s3.amazonaws.com/cc25d8562c9611e3a8b922000a1f8ac2_8.jpg 
http://distilleryimage7.s3.amazonaws.com/4d8e22da2c8911e3a6a022000ae81e78_8.jpg 
http://distilleryimage5.s3.amazonaws.com/ce6aa38a2be711e391ae22000ae9112d_8.jpg 
http://distilleryimage3.s3.amazonaws.com/d64ab4c42bc811e39cbd22000a1fafdb_8.jpg 
...... 
...... 
+0

谢谢! 如果我想在相同的数组中发布链接,说明和时间,我该怎么办? 输出: 1. 链路:BLA 描述:XX 时间:XX 2.链路 描述:XX 时间:XX –

+0

'的foreach($ HTML->找到( '项目')为$ EL) \t { \t echo $ el-> find('description img',0) - > src。 “
”; \t echo $ el-> find('link',0) - > innertext。 “
”; \t echo $ el-> find('pubDate',0) - > innertext。 “
”; \t}' –

+0

[阅读文档](http://simplehtmldom.sourceforge.net/manual.htm#section_quickstart)。 –