PHP 采集獲取指定網(wǎng)址的內(nèi)容
更新時(shí)間:2010年01月05日 23:00:37 作者:
用php來(lái)獲取指定網(wǎng)頁(yè)內(nèi)容的實(shí)現(xiàn)代碼,一般采集程序經(jīng)常用的一些代碼,這里只是一個(gè)簡(jiǎn)單的基礎(chǔ)。更詳細(xì)的資料可以參考php 采集成品,可以使用的源碼,這樣才能學(xué)會(huì)更多的東西。
參考別人想法變成自己的想法,你會(huì)發(fā)現(xiàn)慢慢下來(lái)以后你就擁有了臨時(shí)解決很多問(wèn)題的思路與方法。
<?php
/*
功能:獲取頁(yè)面內(nèi)容,存儲(chǔ)下來(lá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"; //存儲(chǔ)名
$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
/*
功能:獲取頁(yè)面內(nèi)容,存儲(chǔ)下來(lá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"; //存儲(chǔ)名
$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");
?>
您可能感興趣的文章:
- 開(kāi)啟CURL擴(kuò)展,讓服務(wù)器支持PHP curl函數(shù)(遠(yuǎn)程采集)
- 基于PHP的cURL快速入門教程 (小偷采集程序)
- PHP采集利器 Snoopy 試用心得
- php file_get_contents函數(shù)輕松采集html數(shù)據(jù)
- 基于PHP的簡(jiǎn)單采集數(shù)據(jù)入庫(kù)程序
- PHP實(shí)現(xiàn)采集抓取淘寶網(wǎng)單個(gè)商品信息
- PHP 采集程序 常用函數(shù)
- snoopy 強(qiáng)大的PHP采集類使用實(shí)例代碼
- PHP采集類snoopy詳細(xì)介紹(snoopy使用教程)
- 利用PHP命令行模式采集股票趨勢(shì)信息
相關(guān)文章
PHP封裝的遠(yuǎn)程抓取網(wǎng)站圖片并保存功能類
這篇文章主要介紹了PHP封裝的遠(yuǎn)程抓取網(wǎng)站圖片并保存功能類,結(jié)合實(shí)例形式分析了php抓取遠(yuǎn)程圖片封裝類的定義與簡(jiǎn)單使用方法,涉及php正則匹配與文件讀寫(xiě)相關(guān)操作技巧,需要的朋友可以參考下2018-06-06PHP pthreads v3下的Volatile簡(jiǎn)介與使用方法示例
這篇文章主要介紹了PHP pthreads v3下的Volatile簡(jiǎn)介與使用方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了PHP pthreads v3下Volatile的功能、原理、使用方法及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2020-02-02php版交通銀行網(wǎng)銀支付接口開(kāi)發(fā)入門教程
這篇文章主要介紹了php版交通銀行網(wǎng)銀支付接口開(kāi)發(fā)方法,結(jié)合簡(jiǎn)單實(shí)例形式分析了php操作交通銀行網(wǎng)銀接口的的開(kāi)發(fā)步驟與相關(guān)操作技巧,需要的朋友可以參考下2016-09-09基于PHPexecl類生成復(fù)雜的報(bào)表表頭示例
這篇文章主要介紹了基于PHPexecl類生成復(fù)雜的報(bào)表表頭功能,結(jié)合實(shí)例形式分析了實(shí)例化PHPexecl類生成復(fù)雜報(bào)表表頭的具體步驟與相關(guān)操作技巧,需要的朋友可以參考下2016-10-10PHP給前端返回一個(gè)JSON對(duì)象的實(shí)例講解
今天小編就為大家分享一篇PHP給前端返回一個(gè)JSON對(duì)象的實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-05-05Look And Say 序列php實(shí)現(xiàn)代碼
Look And Say序列,簡(jiǎn)單的說(shuō)就是根據(jù)你看到的數(shù)字,寫(xiě)出下一個(gè)數(shù)字2011-05-05判斷Keep-Alive模式的HTTP請(qǐng)求的結(jié)束的實(shí)現(xiàn)代碼
在使用短連接方式時(shí),每個(gè)HTTP請(qǐng)求對(duì)應(yīng)一個(gè)TCP連接,請(qǐng)求完成后連接立即斷開(kāi),服務(wù)器返回EOF。2011-08-08PHP swoole中http_server的配置與使用方法實(shí)例分析
這篇文章主要介紹了PHP swoole中http_server的配置與使用方法,結(jié)合實(shí)例形式分析了swoole中swoole_http_server類的功能及http_server的配置、創(chuàng)建服務(wù)相關(guān)使用技巧,需要的朋友可以參考下2020-03-03