0
我繼NETTUTS刮教程的簡化版本,在這裏,這基本上找到所有的div class=preview
簡單的HTML DOM只得到1元
這是我的代碼。問題是,當我數$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);
建議第三方替代[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
如果你做了'$ items [] = $ html-> find('div [class = preview]');'或者剛剛聲明瞭數組,它要麼不正確地抓取DOM,要麼不正確地存儲它。可以試試'var_dump($ html-> find('div [class = preview]'))' –
您可以比較simplehtmldom phpquery和ganon的選擇語法[here](http://scraperblog.blogspot.com/2012/ 11 /選擇-PHP-HTML-parser.html)。我發現phpquery具有最清晰的語法,並且總體上是最好的。 – pguardiario