0

我在这里关注 NetTuts 的一个简化版的抓取教程,它基本上可以找到所有 divclass=preview

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

这是我的代码。问题是当我数数时$items我只得到 1,所以它只得到第一个 div class=preview,而不是全部。

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

1 回答 1

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; }
于 2011-09-22T21:33:35.463 回答