大家用PHP爬取数据的时候,一般使用 preg_match_all 函数, 经常需要用到正则匹配,而很多人正则没有学好,所以写爬虫就很麻烦。python做爬虫之所以方便,最主要的是有封装好的DOM结构包,这样写爬虫的时候直接使用前端的DOM方式就能很快定位,无需写正则匹配。那么PHP有没有封装好的DOM类呢?
答案是有的。今天就给大家介绍这个简单小巧的PHP封装的DOM结构类-simple_html_dom。
objectstr_get_html ( string $content ) 从字符串创建DOM对象 objectfile_get_html ( string $filename ) 从URL或者文件创建DOM DOM 方法和 & 属性 void__construct ( [string $filename] ) 构造方法, 参数可以是文件名、URL、HTML字符串,或者不传参数。 stringplaintext 返回提取的HTML文本内容. voidclear () 释放对象占用的内存 voidload ( string $content ) 加载字符串用于解析 stringsave ( [string $filename] ) 返回内部DOM树的字符串,如果传递了文件名参数,将把内部DOM树的字符串存储到文件 voidload_file ( string $filename ) 加载一个文件或者URL内容用于解析 voidset_callback ( string $function_name ) 设置回调函数 mixedfind ( string $selector [, int $index] ) 使用css选择器查找一组元素.第二个参数是数组索引,从0开始。 没有第二个参数返回的是找的所有元素的数组,如果传递了第二个参数返回数组中索引位置的元素对象。 Element元素 方法 & 属性 string[attribute] 获取或者设置元素的属性值 stringtag 获取或者设置元素的标签名 stringoutertext 获取或者设置元素的 outer HTML stringinnertext 获取或者设置元素的 inner HTML stringplaintext 获取或者设置元素的纯文本 mixedfind ( string $selector [, int $index] ) 使用css选择器查找一组元素.第二个参数是数组索引,从0开始。 没有第二个参数返回的是找的所有元素的数组,如果传递了第二个参数返回数组中索引位置的元素对象。 DOM 遍历 mixed$e->children ( [int $index] ) 无参数时返回所有的后代元素数组,参数是具体后代元素数组的索引,如果传递了索引参数那么返回的是一个DOM对象而不是数组。 element$e->parent () 返回父元素 element$e->first_child () 返回第一个子元素,不存在就返回null element$e->last_child () 返回最后一个子元素,不存在就返回null element$e->next_sibling () 返回下一个同级元素,不存在就返回null element$e->prev_sibling () 返回上一个同级元素,不存在就返回null 驼峰命名的方法 对应的等效方法 array$e->getAllAttributes () array$e->attr string$e->getAttribute ( $name ) string$e->attribute void$e->setAttribute ( $name, $value ) void$value = $e->attribute bool$e->hasAttribute ( $name ) boolisset($e->attribute) void$e->removeAttribute ( $name ) void$e->attribute = null element$e->getElementById ( $id ) mixed$e->find ( "#$id", 0 ) mixed$e->getElementsById ( $id [,$index] ) mixed$e->find ( "#$id" [, int $index] ) element$e->getElementByTagName ($name ) mixed$e->find ( $name, 0 ) mixed$e->getElementsByTagName ( $name [, $index] ) mixed$e->find ( $name [, int $index] ) element$e->parentNode () element$e->parent () mixed$e->childNodes ( [$index] ) mixed$e->children ( [int $index] ) element$e->firstChild () element$e->first_child () element$e->lastChild () element$e->last_child () element$e->nextSibling () element$e->next_sibling () element$e->previousSibling () element$e->prev_sibling ()
使用示例如下:
//使用案例---抓取360视频的全网视频列表 //首先引入DOM类 include("simple_html_dom.php"); $url = "https://video.360kan.com/v?ie=utf-8&src=360sou_home&q=小明&_re=0"; $html = file_get_html($url); $a_s = $html->find('.w-sfigure-imglink'); $img_s = $html->find('.w-sfigure-imglink img'); $t_s = $html->find('.w-sfigure-title'); $res = array(); foreach($a_s as $k=>$v){ $res[$k]['urls'] = $v->href; } foreach($img_s as $k=>$v){ $res[$k]['imgs'] = $v->getAttribute('data-src');//抓取其中的元素值 } foreach($t_s as $k=>$v){ $res[$k]['titles'] = $v->innertext; } $l = '<div class="b-listtab-main"><div class="s-tab-main"><ul class="list g-clear">'; foreach($res as $k=>$v){ $l.='<li class="item"><a class="js-tongjic" href="splay.php?url='.$v['urls'].'&id='.$k.'" title="'.$v['titles'].'" target="_blank"> <div class="cover g-playicon"> <img src="'.$v['imgs'].'" alt="'.$v['titles'].'"> </div> <div class="detail"> <p class="title g-clear"> <span class="s1">'.$v['titles'].'</span></p> </div> </a></li>'; } $l.='</ul></div></div>'; echo $l;
由于篇幅问题,我这里就不多介绍了,这里有一份详细文档,大家可以直接查看:simple_html_dom文档说明
文件下载:simple_html_dom.zip