PHP 采集网页文字并保存的实现方法

前端之家收集整理的这篇文章主要介绍了PHP 采集网页文字并保存的实现方法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
PHP采集网页文字采集并保存,感兴趣的小伙伴,下面一起跟随编程之家 jb51.cc的小编来看看吧。
经测试代码如下:

<?PHP
/**
 *功能:获取页面内容,存储下来阅读;lost63
 *
 * @param 
 * @author 编程之家 jb51.cc jb51.cc
 **/
 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,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>/',$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://jb51.cc /jiaocheng/javascript-jiaocheng-352.html");
 ?>


/***   来自编程之家 jb51.cc(jb51.cc)   ***/
原文链接:https://www.f2er.com/php/529118.html

猜你在找的PHP相关文章