首頁 後端開發 php教程 一个数据采集类_php技巧

一个数据采集类_php技巧

May 17, 2016 am 09:41 AM

复制代码 代码如下:


// 兼容 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>


      
";
        echo $tempstr;
    }


}
?>

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
1 個月前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
1 個月前 By 尊渡假赌尊渡假赌尊渡假赌
威爾R.E.P.O.有交叉遊戲嗎?
1 個月前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

在PHP API中說明JSON Web令牌(JWT)及其用例。 在PHP API中說明JSON Web令牌(JWT)及其用例。 Apr 05, 2025 am 12:04 AM

JWT是一種基於JSON的開放標準,用於在各方之間安全地傳輸信息,主要用於身份驗證和信息交換。 1.JWT由Header、Payload和Signature三部分組成。 2.JWT的工作原理包括生成JWT、驗證JWT和解析Payload三個步驟。 3.在PHP中使用JWT進行身份驗證時,可以生成和驗證JWT,並在高級用法中包含用戶角色和權限信息。 4.常見錯誤包括簽名驗證失敗、令牌過期和Payload過大,調試技巧包括使用調試工具和日誌記錄。 5.性能優化和最佳實踐包括使用合適的簽名算法、合理設置有效期、

描述紮實的原則及其如何應用於PHP的開發。 描述紮實的原則及其如何應用於PHP的開發。 Apr 03, 2025 am 12:04 AM

SOLID原則在PHP開發中的應用包括:1.單一職責原則(SRP):每個類只負責一個功能。 2.開閉原則(OCP):通過擴展而非修改實現變化。 3.里氏替換原則(LSP):子類可替換基類而不影響程序正確性。 4.接口隔離原則(ISP):使用細粒度接口避免依賴不使用的方法。 5.依賴倒置原則(DIP):高低層次模塊都依賴於抽象,通過依賴注入實現。

解釋PHP中晚期靜態結合的概念。 解釋PHP中晚期靜態結合的概念。 Mar 21, 2025 pm 01:33 PM

文章討論了PHP 5.3中介紹的PHP中的晚期靜態結合(LSB),允許靜態方法的運行時間分辨率調用以更靈活的繼承。 LSB的實用應用和潛在的觸摸

如何在系統重啟後自動設置unixsocket的權限? 如何在系統重啟後自動設置unixsocket的權限? Mar 31, 2025 pm 11:54 PM

如何在系統重啟後自動設置unixsocket的權限每次系統重啟後,我們都需要執行以下命令來修改unixsocket的權限:sudo...

如何用PHP的cURL庫發送包含JSON數據的POST請求? 如何用PHP的cURL庫發送包含JSON數據的POST請求? Apr 01, 2025 pm 03:12 PM

使用PHP的cURL庫發送JSON數據在PHP開發中,經常需要與外部API進行交互,其中一種常見的方式是使用cURL庫發送POST�...

框架安全功能:防止漏洞。 框架安全功能:防止漏洞。 Mar 28, 2025 pm 05:11 PM

文章討論了框架中的基本安全功能,以防止漏洞,包括輸入驗證,身份驗證和常規更新。

自定義/擴展框架:如何添加自定義功能。 自定義/擴展框架:如何添加自定義功能。 Mar 28, 2025 pm 05:12 PM

本文討論了將自定義功能添加到框架上,專注於理解體系結構,識別擴展點以及集成和調試的最佳實踐。

See all articles