2011-09-22 68 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; }