2011-09-22 40 views
0

我繼NETTUTS刮教程的簡化版本,在這裏,這基本上找到所有的div class=preview簡單的HTML DOM只得到1元

http://net.tutsplus.com/tutorials/php/html-parsing-and-screen-scraping-with-the-simple-html-dom-library/comment-page-1/#comments

這是我的代碼。問題是,當我數$items我只得到1,所以它只獲得class=preview,而不是所有的第一個div。

$articles = array(); 
$html = new simple_html_dom(); 
$html->load_file('http://net.tutsplus.com/page/76/'); 

$items = $html->find('div[class=preview]'); 
echo "count: " . count($items); 
+0

建議第三方替代[SimpleHtmlDom(http://simplehtmldom.sourceforge.net/)實際使用[DOM(HTTP:// php.net/manual/en/book.dom.php)而不是字符串分析:[phpQuery](http://code.google.com/p/phpquery/),[Zend_Dom](http://framework.zend .com/manual/en/zend.dom.html),[QueryPath](http://querypath.org/)和[FluentDom](http://www.fluentdom.org)。 – Gordon

+0

如果你做了'$ items [] = $ html-> find('div [class = preview]');'或者剛剛聲明瞭數組,它要麼不正確地抓取DOM,要麼不正確地存儲它。可以試試'var_dump($ html-> find('div [class = preview]'))' –

+0

您可以比較simplehtmldom phpquery和ganon的選擇語法[here](http://scraperblog.blogspot.com/2012/ 11 /選擇-PHP-HTML-parser.html)。我發現phpquery具有最清晰的語法,並且總體上是最好的。 – pguardiario

回答

1

嘗試使用DOMDocumentDOMXPath

$file = file_get_contents('http://net.tutsplus.com/page/76/'); 
$dom = new DOMDocument(); 
@$dom->loadHTML($file); 
$domx = new DOMXPath($dom); 
$nodelist = $domx->evaluate("//div[@class='preview']"); 
foreach ($nodelist as $node) { print $node->nodeValue; }