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");
?>
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.