Powerful PHP tool: Snoopy trial

Source: Internet
Author: User
Powerful PHP collection tool: Snoopy is a php class used to simulate the function of a browser. it can obtain webpage content and send forms. To run Snoopy correctly, the PHP version of your server is later than 4, and supports PCRE (PerlCompatibleRegu PHP collection tool: Snoopy trial

Snoopy is a php class used to simulate browser functions. it can obtain webpage content and send forms. To run Snoopy correctly, the PHP version of your server is later than 4 and PCRE (Perl Compatible Regular Expressions) is supported. basic LAMP services are supported.

I. Some features of Snoopy:

1. fetch the webpage content

2. capture the text content of the webpage (remove HTML tags) fetchtext

3. capture the link of the webpage, form fetchlinks fetchform

4. support for proxy hosts

5. support for basic user name/password verification

6. you can set user_agent, referer, cookies, and header content)

7. supports browser redirection and can control the depth of redirection.

8. extend the link in the webpage to a high-quality url (default)

9. submit data and obtain the returned value

10. support tracking HTML framework

11. cookies can be transferred when redirection is supported. you only need php4 or above. because it is a php class, you do not need to expand the support. The server is the best choice when curl is not supported.

II. Category methods:

Fetch ($ URI)
----

This method is used to capture the content of a webpage. $ URI is the URL of the webpage to be crawled. The captured results are stored in $ this-> results. If you are capturing a framework, Snoopy will track each frame and store it in an array, and then save it to $ this-> results.

Fetchtext ($ URI)
-----

This method is similar to fetch (). The only difference is that this method will remove HTML tags and other irrelevant data and only return the text content in the webpage.

Fetchform ($ URI)
-----

This method is similar to fetch (). The only difference is that this method will remove the HTML tag and other irrelevant data and only return the form content (form) in the webpage ).

Fetchlinks ($ URI)
------

This method is similar to fetch (). The only difference is that this method will remove HTML tags and other irrelevant data and only return links in the webpage ).
By default, the relative link is automatically completed and converted to a complete URL.

Submit ($ URI, $ formvars)
--------

This method sends a confirmation form to the URL specified by $ URL. $ Formvars is an array that stores form parameters.

Submittext ($ URI, $ formvars)
---------

This method is similar to submit (). The only difference is that this method will remove HTML tags and other irrelevant data and only return the text content on the webpage after login.

Submitlinks ($ URI)
------

This method is similar to submit (). The only difference is that this method will remove HTML tags and other irrelevant data and only return links in the webpage ).
By default, the relative link is automatically completed and converted to a complete URL.

III. class attributes: (the default value is in brackets)

$ Host connected host
$ Port connection port
$ Proxy_host: the proxy host used, if any
$ Proxy_port indicates the proxy Host Port used. If yes
$ Agent User proxy disguise (Snoopy v0.1)
$ Referer information, if any
$ Cookies, if any
$ Rawheaders other header information, if any
$ Maxredirs maximum redirect times, 0 = not allowed (5)
$ Offsiteok whether or not to allow redirects off-site. (true)
$ Expandlinks: whether to add all links to the full address (true)
$ User authentication username, if any
$ Pass authentication username, if any
$ Accept http accept type (image/gif, image/x-xbitmap, image/jpeg, image/pjpeg ,*/*)
$ Error: where is the error reported? if Yes
$ Response_code response code returned from the server
$ Headers header information returned from the server
$ Maxlength: maximum length of returned data
$ Read_timeout read operation timeout (requires PHP 4 Beta 4 +)
Set 0 to no timeout
$ Timed_out if a read operation times out, this attribute returns true (requires PHP 4 Beta 4 +)
$ Maxframes maximum number of frames that can be tracked
$ Status indicates the http status captured.
$ Temp_dir temporary file directory (/tmp) that can be written by the webpage server)
$ Curl_path cURL binary Directory. If no cURL binary is available, set it to false.

4. demo

Include "Snoopy. class. php "; $ snoopy = new Snoopy; $ snoopy-> proxy_host =" www.baidu.com "; $ snoopy-> proxy_port =" 8080 "; $ snoopy-> agent =" (compatible; MSIE 4.01; MSN 2.5; AOL 4.0; Windows 98) "; $ snoopy-> referer =" http://www.baidu.com/"; $ snoopy-> cookies [" SessionID "] = 238472841723489l; $ snoopy-> cookies ["favoriteColor"] = "RED"; $ snoopy-> rawheaders ["Pragma"] = "no-cache"; $ snoopy-> maxredirs = 2; $ snoopy-> offsiteok = false; $ snoopy-> expandlinks = false; $ snoopy-> user = "joe"; $ snoopy-> pass = "bloe "; if ($ snoopy-> fetchtext ("http://www.baidu.com") {echo"
".htmlspecialchars($snoopy->results)." 
\ N ";
}
Else
Echo "error fetching document:". $ snoopy-> error. "\ n"; snoopy collects phpchina samples Submit ($ submit_url, $ submit_vars); if ($ snoopy-> results) {// Obtain the connection address $ snoopy-> fetchlinks (" http://www.phpchina.com/bbs "); $ Url = array (); $ url = $ snoopy-> results; // print_r ($ url); foreach ($ url as $ key => $ value) {// Match http://www.phpchina.com/bbs /Forumdisplay. php? Fid = 156 & sid = VfcqTR address: Forum forum address if (! Preg_match ("/^ (http: \/www \. phpchina \. com \/bbs \/forumdisplay \. php \? Fid =) [0-9] * & sid = [a-zA-Z] {6}/I ", $ value )) {unset ($ url [$ key]) ;}// print_r ($ url); // you can obtain the plate array $ url and access it cyclically, obtain the data on the first page of the first module $ I = 0; foreach ($ url as $ key => $ value) {if ($ I> = 1) {// test limit break;} else {// access this module, extract the Post connection address, and extract the Post paging data during official access, then, based on the paging data, extract the Post data $ snoopy = new Snoopy (); $ snoopy-> fetchlinks ($ value); $ tie = array (); $ tie [$ I] = $ snoopy-> results; // print_r ($ tie ); // Convert the array foreach ($ tie [$ I] as $ key => $ value) {// Match http://www.phpchina.com/bbs /Viewthread. php? Tid = 68127 & extra = page % 3D1 & page = 1 & sid = iBLZfK if (! Preg_match ("/^ (http: \/www \. phpchina \. com \/bbs \/viewthread \. php \? Tid =) [0-9] * & extra = page \ % 3D1 & page = [0-9] * & sid = [a-zA-Z] {6}/I ", $ value) {unset ($ tie [$ I] [$ key]) ;}// print_r ($ tie [$ I]); // category array, put the content of different pages of the same post in an array $ left = ''; // connect to the left public address $ j = 0; $ page = array (); foreach ($ tie [$ I] as $ key => $ value) {$ left = substr ($ value,); $ m = 0; foreach ($ tie [$ I] as $ pkey => $ pvalue) {// restructured the array if (substr ($ pvalue,) = $ left) {$ page [$ j] [$ m] = $ pvalue; $ m ++ ;}}$ j ++ ;}// start with removing repeated items // $ page = array _ Unique ($ page); can only be used for one-dimensional arrays $ paget [0] = $ page [0]; $ nums = count ($ page); for ($ n = 1; $ n <$ nums; $ n ++) {$ paget [$ n] = array_diff ($ page [$ n], $ page [$ n-1]);} // remove the multi-dimensional array repetition value to end // remove the array null value unset ($ page); $ page = array (); // redefine the page array $ page = array_filter ($ paget); // print_r ($ page); $ u = 0; $ title = array (); $ content = array (); $ temp = ''; $ tt = array (); foreach ($ page as $ key => $ value) {// peripheral loop, for a post if (is_array ($ value) {foreach ($ value As $ k1 => $ v1) {// page loop. for N pages of a post, $ snoopy = new Snoopy (); $ snoopy-> fetch ($ v1 ); $ temp = $ snoopy-> results; // read the title if (! Preg_match_all ("/(. *) <\/h2>/I ", $ temp, $ tt) {echo" no title "; exit ;} else {$ title [$ u] = $ tt [1] [1];} unset ($ tt); // read content if (! Preg_match_all ("/(. *) <\/div>/I ", $ temp, $ tt) {print_r ($ tt); echo" no content1 "; exit ;} else {foreach ($ tt [1] as $ c => $ c2) {$ content [$ u]. = $ c2 ;}}} else {// directly retrieve the page content $ snoopy = new Snoopy (); $ snoopy-> fetch ($ value ); $ temp = $ snoopy-> results; // read the title if (! Preg_match_all ("/(. *) <\/h2>/I ", $ temp, $ tt) {echo" no title "; exit ;} else {$ title [$ u] = $ tt [1] [1];} unset ($ tt); // read content if (! Preg_match_all ("/(. *) <\/div>/I ", $ temp, $ tt) {echo" no content2 "; exit ;} else {foreach ($ tt [1] as $ c => $ c2) {$ content [$ u]. =$ c2 ;}}$ u ++;} print_r ($ content) ;}$ I ++ ;}} else {echo "login failed"; exit ;}?>

??

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.