Snoopy是什麼?
Snoopy是一個php類,用來模仿web瀏覽器的功能,它能完成獲取網頁內容和發送表單的任務。
Snoopy的一些特色:
* 方便抓取網頁的內容
* 方便抓取網頁的文本內容 (去除HTML標籤)
* 方便抓取網頁的連接
* 支持代理主機
* 支持基本的用戶名/密碼驗證
* 支持設置 user_agent, referer(來路), cookies 和 header content(頭文件)
* 支持瀏覽器轉向,並能控制轉向深度
* 能把網頁中的連接擴展成高質量的url(默認)
* 方便提交數據而且獲取返回值
* 支持跟蹤HTML框架(v0.92增長)
* 支持再轉向的時候傳遞cookies (v0.92增長)
要想了解的更深刻些,你本身Google一下吧。下面就給幾個簡單的例子: javascript
1獲取指定url內容
php
$url = "http://www.codesky.net"; include("snoopy.php"); $snoopy = new Snoopy; $snoopy->fetch($url); //獲取全部內容 echo $snoopy->results; //顯示結果 $snoopy->fetchtext //獲取文本內容(去掉html代碼) $snoopy->fetchlinks //獲取連接 $snoopy->fetchform //獲取表單
$formvars["username"] = "admin"; $formvars["pwd"] = "admin"; $action = "http://www.codesky.net";//表單提交地址 $snoopy->submit($action,$formvars);//$formvars爲提交的數組 echo $snoopy->results; //獲取表單提交後的 返回的結果 $snoopy->submittext; //提交後只返回 去除html的 文本 $snoopy->submitlinks;//提交後只返回 連接
$formvars["username"] = "admin"; $formvars["pwd"] = "admin"; $action = "http://www.codesky.net"; include "snoopy.php"; $snoopy = new Snoopy; $snoopy->cookies["PHPSESSID"] = 'fc106b1918bd522cc863f36890e6fff7'; //假裝sessionid $snoopy->agent = "(compatible; MSIE 4.01; MSN 2.5; AOL 4.0; Windows 98)"; //假裝瀏覽器 $snoopy->referer = "http://s.jb51.net"; //假裝來源頁地址 http_referer $snoopy->rawheaders["Pragma"] = "no-cache"; //cache 的http頭信息 $snoopy->rawheaders["X_FORWARDED_FOR"] = "127.0.0.101"; //假裝ip $snoopy->submit($action,$formvars); echo $snoopy->results;
$snoopy->proxy_host = "www.codesky.net"; $snoopy->proxy_port = "8080"; //使用代理 $snoopy->maxredirs = 2; //重定向次數 $snoopy->expandlinks = true; //是否補全連接 在採集的時候常常用到 // 例如連接爲 /images/taoav.gif 可改成它的全連接 http://www.codesky.net/images/taoav.gif,這個地方其實能夠在最後輸出的時候用ereg_replace函數本身替換 $snoopy->maxframes = 5 //容許的最大框架數 //注意抓取框架的時候 $snoopy->results 返回的是一個數組 $snoopy->error //返回報錯信息
<? //echo var_dump($_SERVER); include("Snoopy.class.php"); $snoopy = new Snoopy; $snoopy->agent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh- CN; rv:1.9.0.5) Gecko/2008120122 Firefox/3.0.5 FirePHP/0.2.1";//這項是瀏覽器信 息,前面你用什麼瀏覽器查看cookie,就用那個瀏覽器的信息(ps:$_SERVER能夠查看到瀏覽器的信息) $snoopy->referer = "http://bbs.jb51.net/index.php"; $snoopy->expandlinks = true; $snoopy->rawheaders["COOKIE"]="__utmz=17229162.1227682761.29.7.utmccn=(referral)|utmcsr=jb51.net|utmcct=/html/index.html|utmcmd=referral; cdbphpchina_smile=1D2D0D1; cdbphpchina_cookietime=2592000; __utma=233700831.1562900865.1227113506.1229613449.1231233266.16; __utmz=233700831.1231233266.16.8.utmccn=(referral)|utmcsr=localhost:8080|utmcct=/test3.php|utmcmd=referral; __utma=17229162.1877703507.1227113568.1231228465.1231233160.58; uchome_loginuser=sinopf; xscdb_cookietime=2592000; __utmc=17229162; __utmb=17229162; cdbphpchina_sid=EX5w1V; __utmc=233700831; cdbphpchina_visitedfid=17; cdbphpchinaO766uPYGK6OWZaYlvHSuzJIP22VpwEMGnPQAuWCFL9Fd6CHp2e%2FKw0x4bKz0N9lGk; xscdb_auth=8106rAyhKpQL49eMs%2FyhLBf3C6ClZ%2B2idSk4bExJwbQr%2BHSZrVKgqPOttHVr%2B6KLPg3DtWpTMUI4ttqNNVpukUj6ElM; cdbphpchina_onlineusernum=3721"; $snoopy->fetch("http://bbs.jb51.net"); $n=ereg_replace("href="","href="http://bbs.jb51.net/",$snoopy->results ); echo ereg_replace("src="","src="http://bbs.jb51.net/",$n); ?>
這兩句代碼源碼天空,由於採集到的內容全部的HTML源碼地址都是相對連接,因此要替換成絕對連接,這樣就能夠引用論壇的圖片和css樣式了。 css
Snoopy下載html