PHP 採集擷取指定網址的內容
來源:互聯網
上載者:User
參考別人想法變成自己的想法,你會發現慢慢下來以後你就擁有了臨時解決很多問題的思路與方法。
複製代碼 代碼如下:<?php
/*
功能:擷取頁面內容,儲存下來閱讀; lost63
*/
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.ijavascript.cn/jiaocheng/javascript-jiaocheng-352.html");
?>