0

このサイトを解析しようとしています (img リンクを取得するため): http://statigr.am/feed/parishilton

これは私のコードです:

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リンクが欲しい。

4

1 に答える 1

0

すべての画像が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
......
......
于 2013-10-04T20:22:28.993 に答える