Home  >  Article  >  Backend Development  >  Clever use of PHP functions to implement collector_PHP tutorial

Clever use of PHP functions to implement collector_PHP tutorial

WBOY
WBOYOriginal
2016-07-15 13:28:09904browse

After a long period of development of PHP, many users are familiar with PHP. We can now use PHP functions to implement collector programs. What is a collector? It is usually called a thief program. It is mainly used to capture the content of other people's web pages. Regarding the production of a collector, it is actually not difficult. You just need to remotely open the web page to be collected, and then use regular expressions to match the required content. As long as you have a little knowledge of regular expressions, you can make your own collector. .

A few days ago I made a program for serializing novels. Because I was afraid of the trouble of updating, I wrote a collector by the way to collect information from the Eight Route Chinese Network. Comparing functions It's simple, you can't customize the rules, but the general idea is in it, and the custom rules can be expanded by yourself. Using PHP as a collector mainly uses two PHP functions: file_get_contents() and preg_match_all(). The former is for reading web content remotely, but it can only be used in versions above PHP5. The latter is a regular function, used Extract the required content. Let’s walk through the function implementation step by step. Because we are collecting novels, we must first extract the book title, author, and genre. Other information can be extracted as needed.

This is not enough, you also need a cutting PHP function:

<ol class="dp-xml">
<li class="alt"><span><span>function cut($string,$start,$end){     </span></span></li>
<li class="">
<span>$</span><span class="attribute"><font color="#ff0000">message</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">explode</font></span><span>($start,$string);     </span>
</li>
<li class="alt">
<span>$</span><span class="attribute"><font color="#ff0000">message</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">explode</font></span><span>($end,$message[1]); return $message[0];}其中$string为要被切取的内容,$start为开始的地方,$end为结束的地方。取出分类号:     </span>
</li>
<li class=""><span> </span></li>
<li class="alt">
<span>$</span><span class="attribute"><font color="#ff0000">start</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">"Html/Book/"</font></span><span>;     </span>
</li>
<li class="">
<span>$</span><span class="attribute"><font color="#ff0000">end</font></span><span>    </span>
</li>
<li class="alt">
<span>= </span><span class="attribute-value"><font color="#0000ff">"List.shtm"</font></span><span>;     </span>
</li>
<li class="">
<span>$</span><span class="attribute"><font color="#ff0000">typeid</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">cut</font></span><span>($typeid[0][0],$start,$end);     </span>
</li>
<li class="alt">
<span>$</span><span class="attribute"><font color="#ff0000">typeid</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">explode</font></span><span>("/",$typeid);[/php]     </span>
</li>
<li class=""><span> </span></li>
<li class="alt"><span>这样,$typeid[0]就是我们要找的分类号了。方法如下:     </span></li>
<li class=""><span> </span></li>
<li class="alt">
<span>$</span><span class="attribute"><font color="#ff0000">ustart</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">""</font></span><span>";     </span>
</li>
<li class="">
<span>$</span><span class="attribute"><font color="#ff0000">uend</font></span><span>    </span>
</li>
<li class="alt">
<span>= </span><span class="attribute-value"><font color="#0000ff">""</font></span><span>";     </span>
</li>
<li class=""><span>//t表示title的缩写     </span></li>
<li class="alt">
<span>$</span><span class="attribute"><font color="#ff0000">tstart</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">">"</font></span><span>;     </span>
</li>
<li class="">
<span>$</span><span class="attribute"><font color="#ff0000">tend</font></span><span>    </span>
</li>
<li class="alt">
<span>= </span><span class="attribute-value"><font color="#0000ff">"<"</FONT></SPAN><SPAN>;     </SPAN></SPAN><LI class=""><SPAN>//取路径,例如:123.shtm,2342.shtm,233.shtm     </SPAN><LI class=alt><SPAN>preg_match_all("/"[0-9]{1,}.(shtm)"/is",$chapterurl,$url);     </SPAN><LI class=""><SPAN>//取标题,例如:第一章 九世善人     </SPAN><LI class=alt><SPAN>preg_match_all("/</SPAN><STRONG><FONT color=#006699><SPAN class=tag><</SPAN><SPAN class=tag-name>a</SPAN></FONT></STRONG><SPAN> </SPAN><SPAN class=attribute><FONT color=#ff0000>href</FONT></SPAN><SPAN>="[0-9]{1,}.shtm"(.*?)</SPAN><SPAN class=tag><STRONG><FONT color=#006699><</FONT></STRONG></SPAN><SPAN>/a</SPAN><SPAN class=tag><STRONG><FONT color=#006699>></font></span><span>/is",$file,$title);     </span>
</li>
<li class="">
<span>$</span><span class="attribute-value"><font color="#0000ff">count</font></span><span class="attribute"><font color="#ff0000">countcount</font></span><span> = count($url[0]);     </span>
</li>
<li class="alt">
<span>for($</span><span class="attribute"><font color="#ff0000">i</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">0</font></span><span>;$i</span><span class="tag"><strong><font color="#006699"><</font></strong></span><span>=$count;$i++)     </span>
</li>
<li class=""><span>{     </span></li>
<li class="alt">
<span>$</span><span class="attribute"><font color="#ff0000">u</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">cut</font></span><span>($url[0][$i],$ustart,$uend);     </span>
</li>
<li class="">
<span>$</span><span class="attribute"><font color="#ff0000">t</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">cut</font></span><span>($title[0][$i],$tstart,$tend);     </span>
</li>
<li class="alt"><span>$array[$u] = $t;     </span></li>
<li class=""><span>}    </span></li>
</ol>

The $array array is all the chapter addresses. At this point, the collector is half done, and the rest is Open each chapter address in a loop, read, and then match the content. This is relatively simple and will not be described in detail here. Okay, let’s end it today. This is my first time writing such a long article, so there will inevitably be problems with language organization. Please bear with me!


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446461.htmlTechArticleAfter a long period of development of PHP, many users are familiar with PHP. We can now use PHP functions to implement collectors program. What is a collector? It is usually called a thief program. It is mainly used to catch...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn