Simple HTML DOM gets only 1 element

最后都变了- 提交于 2020-01-07 07:42:12

问题


I'm following a simplified version of the scraping tutorial by NetTuts here, which basically finds all divs with class=preview

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

This is my code. The problem is that when I count $items I get only 1, so it's getting only the first div with class=preview, not all of them.

$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);

回答1:


Try using DOMDocument and DOMXPath:

$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; }


来源:https://stackoverflow.com/questions/7521507/simple-html-dom-gets-only-1-element

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!