一个数据采集类


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中Session的概念
Oct 09 PHP
Discuz板块横排显示图片的实现方法
May 28 PHP
Php获取金书网的书名的实现代码
Jun 11 PHP
浏览器预览PHP文件时顶部出现空白影响布局分析原因及解决办法
Jan 11 PHP
PHP类继承 extends使用介绍
Jan 14 PHP
PHP闭包实例解析
Sep 08 PHP
PHP实现通过中文字符比率来判断垃圾评论的方法
Oct 20 PHP
19个Android常用工具类汇总
Dec 30 PHP
Laravel中使用阿里云OSS Composer包分享
Feb 10 PHP
codeigniter发送邮件并打印调试信息的方法
Mar 21 PHP
PHP分页初探 一个最简单的PHP分页代码的简单实现
Jun 21 PHP
详解Laravel框架的依赖注入功能
May 27 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二维数组用键名分组相加实例函数
2013/11/06 PHP
PHP采用curl模仿用户登陆新浪微博发微博的方法
2014/11/07 PHP
php实现插入排序
2015/03/29 PHP
详解WordPress中的头像缓存和代理中的缓存更新方法
2016/03/01 PHP
ThinkPHP3.1.2 使用cli命令行模式运行的方法
2020/04/14 PHP
判断控件是否已加载完成的代码
2010/02/24 Javascript
基于jQuery替换table中的内容并显示进度条的代码
2011/08/02 Javascript
javascript 数组排序函数sort和reverse使用介绍
2013/11/21 Javascript
jquery过滤特殊字符',防sql注入的实现方法
2016/08/17 Javascript
jQuery实现ajax无刷新分页页码控件
2017/02/28 Javascript
详解基于Vue cli生成的Vue项目的webpack4升级
2018/06/19 Javascript
Node.js对MongoDB进行增删改查操作的实例代码
2019/04/18 Javascript
js中延迟加载和预加载的具体使用
2021/01/14 Javascript
vue3 watch和watchEffect的使用以及有哪些区别
2021/01/26 Vue.js
Python入门篇之对象类型
2014/10/17 Python
Python实现查找系统盘中需要找的字符
2015/07/14 Python
python 基本数据类型占用内存空间大小的实例
2018/06/12 Python
python使用Matplotlib画条形图
2020/03/25 Python
Python Tkinter 简单登录界面的实现
2019/06/14 Python
Django 查询数据库并返回页面的例子
2019/08/12 Python
Python多线程实现支付模拟请求过程解析
2020/04/21 Python
Python ADF 单位根检验 如何查看结果的实现
2020/06/03 Python
python写文件时覆盖原来的实例方法
2020/07/22 Python
基于CSS3实现图片模糊过滤效果
2015/11/19 HTML / CSS
Laura Mercier官网:彩妆大师罗拉玛斯亚的化妆品牌
2018/01/04 全球购物
C# .NET面试题
2015/11/28 面试题
秋季运动会加油稿200字
2014/01/11 职场文书
项目采购员岗位职责
2014/04/15 职场文书
垃圾桶标语
2014/06/24 职场文书
法定代表人授权委托书范本
2014/10/07 职场文书
党的群众路线教育实践活动整改方案
2014/10/28 职场文书
2014年乡镇人大工作总结
2014/11/25 职场文书
初中同学会致辞
2015/08/01 职场文书
2019年浪漫婚礼证婚词
2019/06/27 职场文书
springboot layui hutool Excel导入的实现
2022/03/31 Java/Android
PostgreSQL逻辑复制解密原理解析
2022/09/23 PostgreSQL