手机
当前位置:查字典教程网 >编程开发 >php教程 >一个数据采集类
一个数据采集类
摘要:复制代码代码如下:*/classugs{var$value_;//'目标内容var$src_;//'目标URL地址functionsetur...

复制代码 代码如下:

<?

//兼容php4php5

//程序作者张建52linux.com(我爱Linux)

//联系方法733905@qq.comQQ733905

//简单调用方法

/*

<?

include("ugs.php");//你可以下载本ugs.phps然后重命名为ugs.php

$ugs=newugs();

$url="http://domainname.com/path_to_your_target?param";

$ugs->seturl($url);

$ugs->gather();

//............这里可以调用本类里的其它方法,对$ugs->value_做调整,

以满足您的要求

$content=$ugs->getcontent();

print($content);

?>

*/

classugs

{

var$value_;//'目标内容

var$src_;//'目标URL地址

functionseturl($url)

{

$this->src_=$url;

}

functiongetcontent()

{

return$this->value_;

}

functiongetfile($url)

//获取目标

{

$url_parsed=parse_url($url);

$host=$url_parsed["host"];

$port=$url_parsed["port"];

if($port==0)$port=80;

$path=$url_parsed["path"];

if(empty($path))

$path="/";

if($url_parsed["query"]!="")

$path.="?".$url_parsed["query"];

$out="GET$pathHTTP/1.0rnHost:$hostrnrn";

$fp=fsockopen($host,$port,$errno,$errstr,30);

fwrite($fp,$out);

$body=false;

while(!feof($fp))

{

$s=fgets($fp,1024);

if($body)$in.=$s;

if($s=="rn")

$body=true;

}

fclose($fp);

return$in;

}

functiongetfile_curl($url)

{

$curl="/usr/local/bin/curl";//pathtoyourcurl

$curl_options="-s--connect-timeout10--max-time10";

//curl用法请参考curl--help或者mancurl

//curl参数非常之丰富,可以模拟各种浏览器(agent)可以设置referer

$cmd="$curl$curl_options$url";

@exec($cmd,$o,$r);

if($r!=0)

{

return"超时";

}

else

{

$o=join("",$o);

return$o;

}

}

functiongather_curl($curl)

{

$http=$this->getfile_curl($this->src_);

return$this->value_=$http;

}

functiongather_array($url)

{

returnfile($url);

}

functiongather()

//开始收集

{

$http=$this->getfile($this->src_);

return$this->value_=$http;

}

functiongather_local($toline=true)

//处理本地文件

{

if($toline)

{

$http=file($this->src_);

return$this->value_=$this->BytesToBstr($http);

}

else

{

$http=file($this->src_);

return$this->value_=$http;

}

}

functionnoReturn()

//删除回车换行

{

$this->value_=str_replace("n","",$this->value_);

$this->value_=str_replace("r","",$this->value_);

}

functionchange($oldStr,$str)

//'对收集到的内容中的个别字符串用新值更换/方法

//'参数分别是旧字符串,新字符串

{

$this->value_=str_replace($oldStr,$str,$this->value_);

}

functioncut($start,$end,$no='1',$comprise='')

//'按指定首尾字符串对收集的内容进行裁减(不包括首尾字符串)方法

//$no必须是1,23...不允许是0

//$comprise可以选择start或者end或者all或者什么都不填

{

$string=explode($start,$this->value_);

//print_r($string);

$string=explode($end,$string[$no]);

//print_r($string);

switch($comprise){

case'start':

$string=$start.$string[0];

break;

case'end':

$string=$string[0].$end;

break;

case'all':

$string=$start.$string[0].$end;

break;

default:

$string=$string[0];

}

return$this->value_=$string;

}

functionfilt($head,$bot,$str,$no='1',$comprise='')

//'按指定首尾字符串对收集的内容用新值进行替换(不包括首尾字符串)方法

//'参数分别是首字符串,尾字符串,新值,新值位空则为过滤

{

$tmp_v=$this->value_;

$tmp=$this->cut($head,$bot,$no,$comprise);

return$this->value_=str_replace($tmp,$str,$tmp_v);

}

functionlocal()

{

//'将收集的内容中的绝对URL地址改为本地相对地址

//还没实现

}

functionreplaceByReg($patrn,$str)

//'对收集的内容中的符合正则表达式的字符串用新值进行替换/方法

//'参数是你自定义的正则表达式,新值

{

return$this->value_=join("",preg_replace($patrn,$str,$this->value_));

}

functiondebug()

//调试显示

{

$tempstr="<SCRIPT>functionrunEx(){varwinEx2=window.open("","winEx2","width=500,height=300,status=yes,menubar=no,scrollbars=yes,resizable=yes");winEx2.document.open("text/html","replace");winEx2.document.write(unescape(event.srcElement.parentElement.children[0].value));winEx2.document.close();}functionsaveFile(){varwin=window.open('','','top=10000,left=10000');win.document.write(document.all.asdf.innerText);win.document.execCommand('SaveAs','','javascript.htm');win.close();}</SCRIPT><center><TEXTAREAid=asdfname=textfieldrows=32wrap=VIRTUALcols="120">".$this->value_."</TEXTAREA><BR><BR><INPUTname=Buttononclick=runEx()type=buttonvalue="查看效果"><INPUTname=Buttononclick=asdf.select()type=buttonvalue="全选"><INPUTname=Buttononclick="asdf.value=''"type=buttonvalue="清空"><INPUTonclick=saveFile();type=buttonvalue="保存代码"></center>";

echo$tempstr;

}

}

?>

【一个数据采集类】相关文章:

一个程序下载的管理程序(三)

一个目录遍历函数

一个用于MySQL的PHP XML类

一个域名查询的程序

一个多文件上传的例子(原创)

多数据表共用一个页的新闻发布

PHP调用三种数据库的方法(2)

一个程序下载的管理程序(一)

一个php作的文本留言本的例子(一)

4.与数据库的连接

精品推荐
分类导航