snoopy是一个php类,用来模仿web浏览器的功能,它能完成获取网页内容和发送表单的任务。
官方网站 http://snoopy.sourceforge.net/
下面是它的一些特征:
方便抓取网页的内容
方便抓取网页的文字(去掉HTML代码)
方便抓取网页的链接
支持代理主机
支持基本的用户/密码认证模式
支持自定义用户agent,referer,cookies和header内容
支持浏览器转向,并能控制转向深度
能把网页中的链接扩展成高质量的url(默认)
方便提交数据并且获取返回值
支持跟踪HTML框架(v0.92增加)
支持再转向的时候传递cookies
下面是简单的例子,比如说我们抓取我的blog的文字
<?php include "Snoopy.class.php"; $snoopy = new Snoopy; $snoopy->fetchtext("http://www.xxxx.com/blog"); echo $snoopy->results; ?> |
^_^,不错把,在比如抓取链接
<?php include "Snoopy.class.php"; $snoopy = new Snoopy; $snoopy->fetchlinks("http://www.xxxx.com/blog"); print_r($snoopy->results); ?> |
<?php include("snoopy.class.php"); $snoopy = new Snoopy; // need an proxy?:// $snoopy->proxy_host = "my.proxy.host"; $snoopy->proxy_port = "8080"; // set browser and referer: $snoopy->agent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)"; $snoopy->referer = "http://www.xxxx.com/"; // set some cookies: $snoopy->cookies["SessionID"] = '238472834723489'; $snoopy->cookies["favoriteColor"] = "blue"; // set an raw-header: $snoopy->rawheaders["Pragma"] = "no-cache"; // set some internal variables: $snoopy->maxredirs = 2; $snoopy->offsiteok = false; $snoopy->expandlinks = false; // set username and password (optional)// $snoopy->user = "joe"; snoopy->pass = "bloe"; // fetch the text of the website www.google.com: if($snoopy->fetchtext("http://www.google.com")) { // other methods: fetch, fetchform, fetchlinks, submittext and submitlinks // response code: print "response code: ".$snoopy->response_code."<br/>\n"; // print the headers: print "<b>Headers:</b><br/>"; while(list($key,$val) = each($snoopy->headers)) { print $key.": ".$val."<br/>\n"; } print "<br/>\n"; // print the texts of the website: print "<pre>".htmlspecialchars($snoopy->results)."</pre>\n"; } else { print "Snoopy: error while fetching document: ".$snoopy->error."\n"; } ?> |
首先,我们要获取到登陆需要发送什么字段,目标地址是什么。这里我们使用snoopy的fetchform来实现。
<? include "Snoopy.class.php"; $snoopy = new Snoopy; $snoopy->fetchform("http://www.xxxx.com/login.php?action=login"); print $snoopy->results; ?> |
当然你也可以直接查看http://www.phpx.com/happy/...的源代码来实现,不过这样更加方便把。这里,我们获取到目标和提交的数据,下一步就可以实现模拟登陆了。代码如下:
<? include "Snoopy.class.php"; $snoopy = new Snoopy; $submit_url = "http://www.xxxx.com/login.php?action=login"; $submit_vars["loginmode"] = "normal"; $submit_vars["styleid"] = "1"; $submit_vars["cookietime"] = "315360000"; $submit_vars["loginfield"] = "username"; $submit_vars["username"] = "********"; //你的用户名 $submit_vars["password"] = "*******"; //你的密码 $submit_vars["questionid"] = "0"; $submit_vars["answer"] = ""; $submit_vars["loginsubmit"] = "提 交"; $snoopy->submit($submit_url,$submit_vars); print $snoopy->results; ?> |
本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!