PHP after a long period of development, many users are very familiar with PHP, we can now use PHP functions to implement the Collector program. What is the collector, usually called the Thief program, mainly used to crawl other people's web content. About the production of the collector, in fact, is not difficult, is to open the Web to be collected remotely, and then use regular expressions to match the required content, as long as a little bit of the basis of regular expression, can make their own collector.
A few days ago did a novel serial program, because afraid of updating trouble, incidentally wrote a collector, the acquisition of eight Chinese network, the function is relatively simple, can not customize the rules, but the idea is in the inside, the custom rules can be extended by themselves. Using PHP to do the collector is mainly used to two PHP functions: file_get_contents () and Preg_match_all (), the previous is to read the Web page content, but only in the version of PHP5 above to use, the latter is a regular function, to extract the required content. The face is a step-by-step function implementation. Because it is a collection of novels, so first of all, the title, author, type of the three extracted, other information can be extracted according to needs.
This is not enough and requires a PHP function to be cut:
- function Cut ($string, $start, $end) {
- $ message = Explode ($start, $string);
- $ message = Explode ($end, $message [1]); return $message [0];} Where $string is the content to be cut, $start the place to start, $end for the end. Remove the classification number:
- $ Start = "html/book/" ;
- $ End
- = "List.shtm" ;
- $ typeID = Cut ($typeid [0][0], $start, $end);
- $ typeID = Explode ("/", $typeid); [/php]
- So, $typeid [0] is the classification number we're looking for. Here's how:
- $ Ustart = "" ";
- $ Uend
- = "" ";
- T denotes the abbreviation of title
- $ Tstart = ">" ;
- $ tend
- = "<" ;
- Take the path, for example: 123.shtm,2342.shtm,233.shtm
- Preg_match_all ("/" [0-9]{1,}. ( shtm) "/is", $chapterurl, $url);
- Title, for example: Chapter Nine The Righteous
- Preg_match_all ("/<a href=" [0-9]{1,}. Shtm "(. *?) < /A > /is ", $file, $title);
- $ Count Countcount = count ($url [0]);
- For ($i=0; $i<= $count; $i + +)
- {
- $ u = Cut ($url [0][$i], $ustart, $uend);
- $ T = Cut ($title [0][$i], $tstart, $tend);
- $array [$u] = $t;
- }
$array array is all the chapter address, here, the collector is half done, the rest is to loop open each chapter address, read, and then match the content. This is relatively simple and is not described in detail here. Well, write this today first, the first time to write such a long article, language organization inevitably have problems, but also please forgive us!
http://www.bkjia.com/PHPjc/446461.html www.bkjia.com true http://www.bkjia.com/PHPjc/446461.html techarticle PHP After a long period of development, many users are very familiar with PHP, we can now use PHP functions to implement the Collector program. What is the collector, usually called the Thief program, is mainly used to catch ...