PHP 采集网页文字并保存的实现方法
内容摘要
这篇文章主要为大家详细介绍了PHP 采集网页文字并保存的实现方法,具有一定的参考价值,可以用来参考一下。
PHP采集网页文字采集并保存,对此感兴趣的朋友,看看idc笔记做的技术笔
PHP采集网页文字采集并保存,对此感兴趣的朋友,看看idc笔记做的技术笔
文章正文
这篇文章主要为大家详细介绍了PHP 采集网页文字并保存的实现方法,具有一定的参考价值,可以用来参考一下。
PHP采集网页文字采集并保存,对此感兴趣的朋友,看看idc笔记做的技术笔记。经测试代码如下:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | <code class = "php" > <?php /** *功能:获取页面内容,存储下来阅读;lost63 * * @param * @author php教程 www.idcnote.com **/ Class GetUrl{ var $url ; //地址 var $result ; //结果 var $content ; //内容 var $list ; //列表 function GetUrl( $url ){ $this ->url= $url ; $this ->GetContent(); $this ->GetList(); $this ->FileSave(); //print_r($this->list[2]); } private function GetContent(){ $this ->result= fopen ( $this ->url, "r" ); while (! feof ( $this ->result)){ $this ->content.= fgets ( $this ->result,9999); } } private function GetList(){ preg_match_all( '/<a(.*?)href="(.*?)">(.*?)</a>/' , $this ->content, $this ->list); $this ->list[2]= array_unique ( $this ->list[2]); //移除相同的值 while (list( $key , $value )=each( $this ->list[2])){ if ( strpos ( $value , ".html" )==0|| strpos ( $value , "jiaocheng" )==0){ unset( $this ->list[2][ $key ]); } else { $this ->list[2][ $key ]= substr ( $value ,0, strpos ( $value , ".html" )). ".html" ; //去掉不需要的标签 } } } private function FileSave(){ foreach ( $this ->list[2] as $value ){ $this ->url= $value ; //重新赋值 $this ->content=null; $this ->GetContent(); //提取内容 preg_match_all( '/<title>(.*?)</title>/' , $this ->content, $files ); //取标题 $filename = $files [1][0]. ".html" ; //存储名 $content = $this ->str_cut( $this ->content, 'http://pagead2.googlesyndication.com/pagead/show_ads.js' , '<div id="article_detail">' ); $file = fopen ( $filename , "w" ); fwrite( $file , $content ); fclose( $file ); echo $filename . "保存 OK<br>n" ; } } function str_cut( $str , $start , $end ) { $content = strstr ( $str , $start ); $content = substr ( $content , strlen ( $start ), strpos ( $content , $end ) - strlen ( $start ) ); return $content ; } } $w = new GetUrl( "http://www.idcnote.com /jiaocheng/javascript-jiaocheng-352.html" ); ?> /*** 来自php教程(www.idcnote.com) ***/ </code> |
注:关于PHP 采集网页文字并保存的实现方法的内容就先介绍到这里,更多相关文章的可以留意
代码注释