一个简单的PHP封装的HTML DOM类,轻松实现PHP爬虫

热度:3792020-03-09 19:31 收藏 0 点赞 0

大家用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



暂无评论
登录后才可以评论~立即登录

如何获取资源?

您可以关注底部公众号,回复文章提示的 “ 关键词 ” 获取对应资源。

每日分享

每日分享收集的网络资源,其中包含开源项目、小工具、常用软件,以及音频视频、电子书籍等。

外卖天天领红包,饿了么,美团红包天天都有!

外卖天天领红包,饿了么,美团红...

最低每顿可省2元

分享两个远程工具-ToDesk和向日葵

分享两个远程工具-ToDesk和向日葵...

在工作生活中经常用到远程,QQ远程很卡,第三方远程工具就比较方便了。

爱奇艺万能播放器2018年完整版

爱奇艺万能播放器2018年完整版

2018年的爱奇艺万能播放器,功能齐全,没有广告!

分享500套个人求职简历模板

分享500套个人求职简历模板

500套免费求职简历下载!

一款最近很火的自动跳过广告APP,无root直装版

一款最近很火的自动跳过广告APP,...

自动跳过广告,无需root

WPS免费去广告电脑软件

WPS免费去广告电脑软件

wps广告实在太多了,下载一个去广告版本