>  기사  >  백엔드 개발  >  php_php 스킬의 스누피 클래스 사용 예

php_php 스킬의 스누피 클래스 사용 예

WBOY
WBOY원래의
2016-05-16 20:13:081060검색

이 글의 예제에서는 PHP에서 스누피 클래스를 사용하는 방법을 설명합니다. 참고할 수 있도록 모든 사람과 공유하세요. 구체적인 분석은 다음과 같습니다.

다음은 PHP에서 스누피를 통해 웹페이지 정보를 크롤링하는 방법을 보여주는 데모입니다

스누피 클래스 다운로드 주소: http://sourceforge.net/projects/snoopy/

/*
You need the snoopy.class.php from 
http://snoopy.sourceforge.net/
*/
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.jonasjohn.de/";
// 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 class="brush:php;toolbar:false">".htmlspecialchars($snoopy->results)."
\n"; } else { print "Snoopy: error while fetching document: ".$snoopy->error."\n"; }

이 기사가 모든 사람의 PHP 프로그래밍 설계에 도움이 되기를 바랍니다.

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.