Example of Snoopy image capturing in PHP

Source: Internet
Author: User
This article mainly introduces the PHP collection class Snoopy image capturing instance. Snoopy is a powerful PHP collection class that can be used to develop some collection programs and thieves programs. For more information, see

This article mainly introduces the PHP collection class Snoopy image capturing instance. Snoopy is a powerful PHP collection class that can be used to develop some collection programs and thieves programs. For more information, see

I used the php Snoopy class for two days and found it useful. You can use fetchlinks to retrieve all the links in the request webpage. You can use fetchtext to obtain all the text information (the regular expression is used internally for processing). There are many other functions, for example, simulate a submission form.


Usage:

First download the Snoopy class ,:
Instantiate an object and call the corresponding method to obtain the captured webpage information.

The Code is as follows:


Include 'snoopy/snoopy. class. php ';

$ Snoopy = new Snoopy ();

$ SourceURL = "http://www.jb51.net ";
$ Snoopy-> fetchlinks ($ sourceURL );

$ A = $ snoopy-> results;

It does not provide a way to get all the image addresses on the webpage. You need to obtain the image addresses in the list of all the articles on the page. Then I wrote one myself, mainly because it is important to match the regular expression.

The Code is as follows:


// Match the regular expression of the image
$ ReTag = "// I ";


Because of the special requirements, you only need to capture the pictures starting with "Dead htp: //" (the pictures on the external site may cause anti-leech protection and want to capture them locally first)

1. Capture the specified webpage and filter out all the expected article addresses;

2. Capture the article address in step 1 cyclically, and then use the regular expression matching the image to obtain the addresses of all the images on the page that comply with the rules;

3. Save the image according to the image suffix and ID (only gif and jpg are available here). If the image file exists, delete it and save it.

The Code is as follows:



<? Php
Include 'snoopy/snoopy. class. php ';

$ Snoopy = new Snoopy ();

$ SourceURL = "http: // xxxxx ";
$ Snoopy-> fetchlinks ($ sourceURL );

$ A = $ snoopy-> results;
$ Re = "/d+.html $ /";

// Filter requests for getting the specified file address
Foreach ($ a as $ tmp ){
If (preg_match ($ re, $ tmp )){
GetImgURL ($ tmp );
}
}

Function getImgURL ($ siteName ){
$ Snoopy = new Snoopy ();
$ Snoopy-> fetch ($ siteName );

$ FileContent = $ snoopy-> results;

// Match the regular expression of the image
$ ReTag = "// I ";

If (preg_match ($ reTag, $ fileContent )){
$ Ret = preg_match_all ($ reTag, $ fileContent, $ matchResult );

For ($ I = 0, $ len = count ($ matchResult [1]); $ I <$ len; ++ $ I ){
SaveImgURL ($ matchResult [1] [$ I], $ matchResult [2] [$ I]);
}
}
}

Function saveImgURL ($ name, $ suffix ){
$ Url = $ name. ".". $ suffix;

Echo "requested image address:". $ url ."
";

$ ImgSavePath = "E:/xxx/style/images /";
$ ImgId = preg_replace ("/^. +/(d +) $/", "\ 1", $ name );
If ($ suffix = "gif "){
$ ImgSavePath. = "emotion ";
} Else {
$ ImgSavePath. = "topic ";
}
$ ImgSavePath. = ("/". $ imgId. ".". $ suffix );

If (is_file ($ imgSavePath )){
Unlink ($ imgSavePath );
Echo"

The file ". $ imgSavePath." already exists and will be deleted.

";
}

$ ImgFile = file_get_contents ($ url );
$ Flag = file_put_contents ($ imgSavePath, $ imgFile );

If ($ flag ){
Echo"

The file ". $ imgSavePath." is saved successfully.

";
}
}
?>

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.