0
点赞
收藏
分享

微信扫一扫

simple-Html-Dom解析HTML文件


要想获得网页源码里的指定内容需要用到正则表达式!正则表达式,让我猝不及防,因为之前没有接触过,用起来非常的吃力!

在Java中,有大牛封装好的库,我使用的是Jsoup。将Jsuop的Jar包引入项目中,指定好唯一标示,再使用选择器,就可以将数据解析出来,最近接触到了PHP,就像使用PHP解析个新闻来玩玩!Jsoup解析滁州学院官网获取信息列表

正如我刚才所说,正规的做法就是使用正则表达式获取数据!搞了好长时间,真心驾驭不住!无论怎样研究,都没有什么卵用!

最后我通过Google搜索引擎,搜索到一篇文章,文章里介绍几种调用PHP文件来解析HTML的方法,真是天助我也!

文章我待会给转载发布出来,因为我找到的也是比人通过“转载器”发表的,既然很有用,我就给它搬过来!

昨天我使用Simple-Html-Dom.php文件,解析糗事百科首页的糗事,并定时,15分钟获取一次!因为刚刚接触PHP,昨天在11点半断网之前刚把代码发布到SAE上,有点担心代码不能正常运行,毕竟对PHP一点都不了解!

今早起来,一看数据库

这酸爽,数据太多了,太多也没用,我就关闭了获取!

下面来讲讲如何使用Simple-Html-Dom来解析HTML(小弟接触php不到两天),如果大牛看到,呵呵一笑,最好给点意见,不要喷,怕被喷!

1、下载Simple-Html-Dom压缩文件

去官网(http://sourceforge.net/projects/simplehtmldom/files/)下载压缩文件;

2、解压文件

解压文件会发现如下文件

你需要用到的方法,demo里基本上都有,就看你怎么使用了

大家看如下代码,会发现是如何引入php文件和方法的

// example of how to use basic selector to retrieve HTML contents 
 include('../simple_html_dom.php');//引入php核心文件,注意路径,不要写错 // get DOM from URL or file 
 $html = file_get_html('http://www.google.com/'); // find all link 
 foreach($html->find('a') as $e) 
 echo $e->href . '
'; // find all image 
 foreach($html->find('img') as $e) 
 echo $e->src . '
'; // find all image with full tag 
 foreach($html->find('img') as $e) 
 echo $e->outertext . '
'; // find all div tags with id=gbar 
 foreach($html->find('div#gbar') as $e) 
 echo $e->innertext . '
'; // find all span tags with class=gb1 
 foreach($html->find('span.gb1') as $e) 
 echo $e->outertext . '
'; // find all td tags with attribite align=center 
 foreach($html->find('td[align=center]') as $e) 
 echo $e->innertext . '
'; // extract text from table 
 echo $html->find('td[align="center"]', 1)->plaintext.''; 
 
// extract text from HTML 
 echo $html->plaintext; 
 ?> 
 还有这个php文件 
    // example of how to use advanced selector features 
 include('../simple_html_dom.php'); // ----------------------------------------------------------------------------- 
 // descendant selector 
 $str = <<  

    ok
   
 
 
 HTML; 
 
$html = str_get_html($str); 
 echo $html->find('div div div', 0)->innertext . '
'; // result: "ok" // ----------------------------------------------------------------------------- 
 // nested selector 
 $str = << • item:1
• item:2
• item:3
• item:4
HTML; 
$html = str_get_html($str); 
 foreach($html->find('ul') as $ul) { 
 foreach($ul->find('li') as $li) 
 echo $li->innertext . '
'; 
 } // ----------------------------------------------------------------------------- 
 // parsing checkbox 
 $str = <<  item1
  item2
  item3
  
 
HTML; 
$html = str_get_html($str); 
 foreach($html->find('input[type=checkbox]') as $checkbox) { 
 if ($checkbox->checked) 
 echo $checkbox->name . ' is checked
'; 
 else 
 echo $checkbox->name . ' is not checked
'; 
 } 
 ?>


这个Demo讲解的更加详细,虽然简洁但是不简单!大家如果感兴趣,可以下载运行一下试试

要想获得网页源码里的指定内容需要用到正则表达式!正则表达式,让我猝不及防,因为之前没有接触过,用起来非常的吃力!

在Java中,有大牛封装好的库,我使用的是Jsoup。将Jsuop的Jar包引入项目中,指定好唯一标示,再使用选择器,就可以将数据解析出来,最近接触到了PHP,就像使用PHP解析个新闻来玩玩!Jsoup解析滁州学院官网获取信息列表

正如我刚才所说,正规的做法就是使用正则表达式获取数据!搞了好长时间,真心驾驭不住!无论怎样研究,都没有什么卵用!

最后我通过Google搜索引擎,搜索到一篇文章,文章里介绍几种调用PHP文件来解析HTML的方法,真是天助我也!

文章我待会给转载发布出来,因为我找到的也是比人通过“转载器”发表的,既然很有用,我就给它搬过来!

昨天我使用Simple-Html-Dom.php文件,解析糗事百科首页的糗事,并定时,15分钟获取一次!因为刚刚接触PHP,昨天在11点半断网之前刚把代码发布到SAE上,有点担心代码不能正常运行,毕竟对PHP一点都不了解!

今早起来,一看数据库

这酸爽,数据太多了,太多也没用,我就关闭了获取!

下面来讲讲如何使用Simple-Html-Dom来解析HTML(小弟接触php不到两天),如果大牛看到,呵呵一笑,最好给点意见,不要喷,怕被喷!

1、下载Simple-Html-Dom压缩文件

去官网(http://sourceforge.net/projects/simplehtmldom/files/)下载压缩文件;

2、解压文件

解压文件会发现如下文件

你需要用到的方法,demo里基本上都有,就看你怎么使用了

大家看如下代码,会发现是如何引入php文件和方法的

// example of how to use basic selector to retrieve HTML contents 
 include('../simple_html_dom.php');//引入php核心文件,注意路径,不要写错 // get DOM from URL or file 
 $html = file_get_html('http://www.google.com/'); // find all link 
 foreach($html->find('a') as $e) 
 echo $e->href . '
'; // find all image 
 foreach($html->find('img') as $e) 
 echo $e->src . '
'; // find all image with full tag 
 foreach($html->find('img') as $e) 
 echo $e->outertext . '
'; // find all div tags with id=gbar 
 foreach($html->find('div#gbar') as $e) 
 echo $e->innertext . '
'; // find all span tags with class=gb1 
 foreach($html->find('span.gb1') as $e) 
 echo $e->outertext . '
'; // find all td tags with attribite align=center 
 foreach($html->find('td[align=center]') as $e) 
 echo $e->innertext . '
'; // extract text from table 
 echo $html->find('td[align="center"]', 1)->plaintext.''; 
 
// extract text from HTML 
 echo $html->plaintext; 
 ?> 
 还有这个php文件 
    // example of how to use advanced selector features 
 include('../simple_html_dom.php'); // ----------------------------------------------------------------------------- 
 // descendant selector 
 $str = <<  

    ok
   
 
 
 HTML; 
 
$html = str_get_html($str); 
 echo $html->find('div div div', 0)->innertext . '
'; // result: "ok" // ----------------------------------------------------------------------------- 
 // nested selector 
 $str = << • item:1
• item:2
• item:3
• item:4
HTML; 
$html = str_get_html($str); 
 foreach($html->find('ul') as $ul) { 
 foreach($ul->find('li') as $li) 
 echo $li->innertext . '
'; 
 } // ----------------------------------------------------------------------------- 
 // parsing checkbox 
 $str = <<  item1
  item2
  item3
  
 
HTML; 
$html = str_get_html($str); 
 foreach($html->find('input[type=checkbox]') as $checkbox) { 
 if ($checkbox->checked) 
 echo $checkbox->name . ' is checked
'; 
 else 
 echo $checkbox->name . ' is not checked
'; 
 } 
 ?>


这个Demo讲解的更加详细,虽然简洁但是不简单!大家如果感兴趣,可以下载运行一下试试


举报

相关推荐

0 条评论