PHP 采集獲取指定網(wǎng)址的內(nèi)容
更新時間:2010年01月05日 23:00:37 作者:
用php來獲取指定網(wǎng)頁內(nèi)容的實現(xiàn)代碼,一般采集程序經(jīng)常用的一些代碼,這里只是一個簡單的基礎(chǔ)。更詳細(xì)的資料可以參考php 采集成品,可以使用的源碼,這樣才能學(xué)會更多的東西。
參考別人想法變成自己的想法,你會發(fā)現(xiàn)慢慢下來以后你就擁有了臨時解決很多問題的思路與方法。
<?php
/*
功能:獲取頁面內(nèi)容,存儲下來閱讀; lost63
*/
Class GetUrl{
var $url; //地址
var $result; //結(jié)果
var $content; //內(nèi)容
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"; //去掉不需要的標(biāo)簽
}
}
}
private function FileSave(){
foreach($this->list[2] as $value){
$this->url=$value; //重新賦值
$this->content=null;
$this->GetContent(); //提取內(nèi)容
preg_match_all('/<title>(.*?)<\/title>/',$this->content,$files); //取標(biāo)題
$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");
?>
復(fù)制代碼 代碼如下:
<?php
/*
功能:獲取頁面內(nèi)容,存儲下來閱讀; lost63
*/
Class GetUrl{
var $url; //地址
var $result; //結(jié)果
var $content; //內(nèi)容
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"; //去掉不需要的標(biāo)簽
}
}
}
private function FileSave(){
foreach($this->list[2] as $value){
$this->url=$value; //重新賦值
$this->content=null;
$this->GetContent(); //提取內(nèi)容
preg_match_all('/<title>(.*?)<\/title>/',$this->content,$files); //取標(biāo)題
$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");
?>
您可能感興趣的文章:
相關(guān)文章
php引用計數(shù)器進(jìn)行垃圾收集機(jī)制介紹
每一種計算機(jī)語言都有自己的自動垃圾回收機(jī)制,讓程序員不必過分關(guān)心程序內(nèi)存分配,php也不例外,但是在面向?qū)ο缶幊?OOP)編程中,有些對象需要顯式的銷毀;防止程序執(zhí)行內(nèi)存溢出2012-09-09
php去除換行符的方法小結(jié)(PHP_EOL變量的使用)
本來在unix世界換行就用/n來代替,但是windows為了體現(xiàn)他的不同,就用/r/n,更有意思的是在mac中用/r。因此unix系列用 /n,windows系列用 /r/n,mac用 /r,這樣就用你寫的程序在不同的平臺上運行有著不少的麻煩2013-02-02

