一个数据采集类


Posted in PHP onFebruary 14, 2007
<? 
// 兼容 php4 php5 
// 程序作者  张建 52linux.com(我爱Linux) 
// 联系方法  733905@qq.com  QQ 733905  
// 简单调用方法 
/* 
<? 
include ("ugs.php"); // 你可以下载本ugs.phps 然后重命名为ugs.php 
$ugs = new ugs(); 
$url = "http://domainname.com/path_to_your_target?param"; 
$ugs->seturl($url); 
$ugs->gather(); 
//............这里可以调用本类里的其它方法,对$ugs->value_  做调整, 
以满足您的要求 
$content=$ugs->getcontent(); 
print($content); 
?> 
*/ 
class ugs 
{ 
    var $value_  ;  //'目标内容 
    var $src_    ;  //'目标URL地址     function seturl($url) 
    { 
        $this->src_=$url; 
    } 
    function getcontent() 
    { 
        return $this->value_; 
    } 
    function getfile($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 $path HTTP/1.0\r\nHost: $host\r\n\r\n"; 
        $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 == "\r\n" ) 
               $body = true; 
        } 
        fclose($fp); 
        return $in; 
   } 
   function getfile_curl($url) 
   { 
    $curl="/usr/local/bin/curl " ; // path to your curl  
    $curl_options=" -s --connect-timeout 10  --max-time 10  ";  
    // curl 用法请参考 curl --help 或者 man curl  
    // curl 参数非常之丰富,可以模拟各种浏览器(agent) 可以设置referer 
    $cmd="$curl $curl_options $url "; 
    @exec($cmd,$o,$r); 
    if($r!=0)  
    { 
        return "超时"; 
    } 
    else 
    { 
        $o=join("",$o); 
        return $o; 
    } 
   } 
    function gather_curl($curl) 
    { 
        $http=$this->getfile_curl($this->src_); 
        return $this->value_=$http; 
    } 
    function gather_array($url) 
     { 
        return file($url); 
    } 
    function   gather() 
     // 开始收集 
    { 
        $http=$this->getfile($this->src_); 
        return $this->value_=$http; 
    } 
    function gather_local($toline=true) 
     // 处理本地文件 
    { 
        if($toline) 
        { 
            $http=file($this->src_); 
            return $this->value_=$this->BytesToBstr($http); 
        } 
        else 
        { 
            $http=file($this->src_); 
            return $this->value_=$http; 
        } 

    } 
    function noReturn() 
     // 删除回车换行 
    { 
        $this->value_=str_replace("\n","",$this->value_); 
        $this->value_=str_replace("\r","",$this->value_); 
    } 

    function change($oldStr,$str) 
    //'对收集到的内容中的个别字符串用新值更换/方法 
    //'参数分别是旧字符串,新字符串 
    { 
        $this->value_=str_replace($oldStr,$str,$this->value_ ); 
    } 

    function cut($start,$end,$no='1',$comprise='') 
    //'按指定首尾字符串对收集的内容进行裁减(不包括首尾字符串)方法 
    // $no 必须是 1,2 3 ... 不允许是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; 
        } 

    function  filt($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); 
    } 

    function  local() 
    { 
        //'将收集的内容中的绝对URL地址改为本地相对地址 
        // 还没实现 
    } 

    function  replaceByReg($patrn,$str) 
     //'对收集的内容中的符合正则表达式的字符串用新值进行替换/方法 
     //'参数是你自定义的正则表达式,新值 
    { 
        return $this->value_=join("",preg_replace($patrn,$str,$this->value_)); 
    } 

    function debug() 
    //调试显示 
    { 
        $tempstr="<SCRIPT>function runEx(){var winEx2 = 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(); }function saveFile(){var win=window.open('','','top=10000,left=10000');win.document.write(document.all.asdf.innerText);win.document.execCommand('SaveAs','','javascript.htm');win.close();}</SCRIPT><center><TEXTAREA id=asdf name=textfield rows=32  wrap=VIRTUAL cols=\"120\">".$this->value_."</TEXTAREA><BR><BR><INPUT name=Button onclick=runEx() type=button value=\"查看效果\">  <INPUT name=Button onclick=asdf.select() type=button value=\"全选\">  <INPUT name=Button onclick=\"asdf.value=''\" type=button value=\"清空\">  <INPUT onclick=saveFile(); type=button value=\"保存代码\"></center>"; 
        echo $tempstr; 
    } 

} 
?>
PHP 相关文章推荐
使用php+xslt在windows平台上
Oct 09 PHP
php学习之数据类型之间的转换代码
May 29 PHP
PHP合并两个数组的两种方式的异同
Sep 14 PHP
ThinkPHP3.1.3版本新特性概述
Jun 19 PHP
PHP中读取照片exif信息的方法
Aug 20 PHP
php压缩和解压缩字符串的方法
Mar 14 PHP
轻松掌握php设计模式之访问者模式
Sep 23 PHP
PHP new static 和 new self详解
Feb 19 PHP
PHP设计模式之工厂模式实例总结
Sep 01 PHP
PHP实现的简单sha1加密功能示例
Aug 27 PHP
tp5(thinkPHP5)框架数据库Db增删改查常见操作总结
Jan 10 PHP
Laravel 6.2 中添加了可调用容器对象的方法
Oct 22 PHP
phpmyadmin中配置文件现在需要绝密的短语密码的解决方法
Feb 11 #PHP
增加反向链接的101个方法 站长推荐
Jan 31 #PHP
Discuz!5的PHP代码高亮显示插件(黑暗中的舞者更新)
Jan 29 #PHP
解决控件遮挡问题:关于有窗口元素和无窗口元素
Jan 28 #PHP
获得Google PR值的PHP代码
Jan 28 #PHP
一键删除顽固的空文件夹 软件下载
Jan 26 #PHP
php中通过smtp发邮件的类,测试通过
Jan 22 #PHP
You might like
PHP从FLV文件获取视频预览图的方法
2015/03/12 PHP
CodeIgniter视图使用注意事项
2016/01/20 PHP
PHP实现广度优先搜索算法(BFS,Broad First Search)详解
2017/09/16 PHP
Yii实现微信公众号场景二维码的方法实例
2020/08/30 PHP
JavaScript获取function所有参数名的方法
2015/10/30 Javascript
原生js编写autoComplete插件
2016/04/13 Javascript
jQuery基础知识点总结(必看)
2016/05/31 Javascript
使用OPENLAYERS3实现点选的方法
2020/09/24 Javascript
javascript实现文字无缝滚动效果
2017/08/26 Javascript
vue单页面实现当前页面刷新或跳转时提示保存
2018/11/02 Javascript
JS数组Object.keys()方法的使用示例
2019/06/05 Javascript
Vue单文件组件开发实现过程详解
2020/07/30 Javascript
elementui实现预览图片组件二次封装
2020/12/29 Javascript
Vue页面渲染中key的应用实例教程
2021/01/12 Vue.js
js实现鼠标切换图片(无定时器)
2021/01/27 Javascript
[10:42]Team Liquid Vs Newbee
2018/06/07 DOTA
简洁的十分钟Python入门教程
2015/04/03 Python
python同时遍历数组的索引和值的实例
2018/11/15 Python
python实现Excel文件转换为TXT文件
2019/04/28 Python
Python多版本开发环境管理工具介绍
2019/07/03 Python
利用python实现周期财务统计可视化
2019/08/25 Python
Python实现socket非阻塞通讯功能示例
2019/11/06 Python
keras 自定义loss层+接受输入实例
2020/06/28 Python
15个应该掌握的Jupyter Notebook使用技巧(小结)
2020/09/23 Python
几个解决兼容IE6\7\8不支持html5标签的几个方法
2013/01/07 HTML / CSS
绢花、人造花和人造花卉:BLOOM
2019/08/07 全球购物
sort命令的作用和用法
2013/08/25 面试题
在职研究生自我鉴定
2013/10/16 职场文书
秸秆管理实施方案
2014/03/15 职场文书
2014年教师节红领巾广播稿
2014/09/10 职场文书
用人单位终止解除劳动合同证明书
2014/10/06 职场文书
2015年学校安全工作总结
2015/04/22 职场文书
交通事故协议书范本
2016/03/19 职场文书
2016年小学“公民道德宣传日”活动总结
2016/04/01 职场文书
Python实战之疫苗研发情况可视化
2021/05/18 Python
OpenCV-Python实现轮廓的特征值
2021/06/09 Python