php中Snoopy类用法实例

这篇文章主要介绍了php中Snoopy类用法,实例分析了使用Snoopy类实现页面抓取的相关技巧,需要的朋友可以参考下,本文实例讲述了php中Snoopy类用法,分享给大家供大家参考,具体分析如下:

这里演示了php中如何通过Snoopy抓取网页信息

snoopy类的下载地址:http://sourceforge.net/projects/snoopy/

  1. /*
  2. You need the snoopy.class.php from
  3. http://snoopy.sourceforge.net/
  4. */
  5. include("snoopy.class.php");
  6. $snoopy = new Snoopy;
  7. // need an proxy?:
  8. //$snoopy->proxy_host = "my.proxy.host";
  9. //$snoopy->proxy_port = "8080";
  10. // set browser and referer:
  11. $snoopy->agent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)";
  12. $snoopy->referer = "http://www.jonasjohn.de/";
  13. // set some cookies:
  14. $snoopy->cookies["SessionID"] = '238472834723489';
  15. $snoopy->cookies["favoriteColor"] = "blue";
  16. // set an raw-header:
  17. $snoopy->rawheaders["Pragma"] = "no-cache";
  18. // set some internal variables:
  19. $snoopy->maxredirs = 2;
  20. $snoopy->offsiteok = false;
  21. $snoopy->expandlinks = false;
  22. // set username and password (optional)
  23. //$snoopy->user = "joe";
  24. //$snoopy->pass = "bloe";
  25. // fetch the text of the website www.google.com:
  26. if($snoopy->fetchtext("http://www.google.com")){
  27. // other methods: fetch, fetchform, fetchlinks, submittext and submitlinks
  28. // response code:
  29. print "response code: ".$snoopy->response_code."<br/>\n";
  30. // print the headers:
  31. print "<b>Headers:</b><br/>";
  32. while(list($key,$val) = each($snoopy->headers)){
  33. print $key.": ".$val."<br/>\n";
  34. }
  35. print "<br/>\n";
  36. // print the texts of the website:
  37. print "<pre>".htmlspecialchars($snoopy->results)."</pre>\n";
  38. }
  39. else {
  40. print "Snoopy: error while fetching document: ".$snoopy->error."\n";
  41. }