近經常需要采集一些網上的數據,發現一個PHP處理HTML的利器 simple html dom,看了一下文檔,使用非常方便,關鍵是能夠用CSS選擇器來訪問DOM樹,和jquery相似,實在是難得的利器.
入門級
simple_html_dom插件
用dom處理html文件的利器
使用:加載simple_html_dom.php文件
require_once 'simple_html_dom.php'
new simple_html_dom對象
$dom = new simple_html_dom()
加載html:
$dom->load($html);
find()方法
$dom->find('div.lookLeftname', 0)->plaintext
class=‘lookLeftname'的div中的純文本
$dom->find('div.lookLeftname', 0)->innertext
class='lookLeftname'的div中的內部文本
完整測試實例,代碼如下:
- <?php
- require_once("simple_html_dom.php");
- ini_set('memory_limit','1000M');
- ini_set("max_execution_time",6000000);
- for($i=1;$i<=21;$i++){
- $html = file_get_html(http://www.111cn.net);
- $as = $html->find('.shopname a');
- $sum = 0;
- foreach($as as $a) {
- if($a->plaintext != "分店"){
- $a->href = "http://www.xx.com".$a->href;
- echo $a->outertext."------".$a->href."-------<br>";
- $sum++;
- }//開源代碼Vevb.com
- }
- echo "<br><br>第 {$i} 頁結束 ,數目:{$sum}<hr>";
- }
- ?>
新聞熱點
疑難解答