This article introduces the content is about the PHP crawler crawl lyrics, has a certain reference value, now share to everyone, the need for friends can refer to
<?phpheader ("Content-type:text/html;charset=utf-8"), class small_crawler{protected $regular; Get content regular protected $url; source protected $i; Control quantity protected $count; Total protected $lyrics _ze; Lyrics content protected $lyrics _LRC; Lyrics address matching protected $txt; Storage address function __construct () {$this->regular = "/<a href=\" \/song\/([0-9]) *\ ". *>*<\/a>/"; $this->lyrics_ze = "/<p id=\" lyriccont\ ". *<\/p>/"; $this->LYRICS_LRC = "/\" http:\/\/.*\ "/"; $this->url = ' http://music.baidu.com/'; $this->txt = "Playlist.txt"; $this->i = 0; $this->count = 110; }/** * Start Task */function perform () {$data = file_get_contents ($this->url); if (Preg_match_all ($this->regular, $data, $mar)) {$this->lyrics ($mar);//Lyrics Filtering}else{Ech O ' no information available '; Exit }}/** * @paRam $data * Lyrics processing * * Function lyrics ($data) {if (Is_array ($data)) {//Lyrics filter FOREAC H ($data [0] as $k = + $y) {$data _s = explode (' "', $y); if (!empty ($data _s[1])) {$the _lyrics = file_get_contents ($this->url. $data _s[1]); if (Preg_match_all ($this->lyrics_ze, $the _lyrics, $lyrics _x)) {if (Preg_match_all ($this->lyrics _LRC, $lyrics _x[0][0], $lyrics _c)) {$lyrics _c = explode (' "', $lyrics _c[0][0]); $lyrics _file = file_get_contents ($lyrics _c[1]); $this->write ($lyrics _file); }}}}//Loop operation foreach ($data [0] as $ki = = $yi) { $data _t = Explode (' "', $yi); if (!empty ($data _t[1])) {$the _lyrics_as = file_get_contents ($this->url. $data _t[1]); if (Preg_match_all ($this->regular, $the _lyrics_as, $mars)) {$this->lyrics ($mars);//Song Word Filter}}}}}/** * @param $lyrics _file * * Write to File * * function write ($lyrics _file) {if ($this->i < $this->count) {$acs = Preg_replace ('/(\[.*\])/ ', ', $lyrics _file); $myfile = fopen ($this->txt, "a"); Fwrite ($myfile, $acs); Fwrite ($myfile, "\ r \ n". $this->i. ". -----------------------------------------------------------\ r \ n "); $this->i++; }else{exit;//End Stop}}}//run $ts = new Small_crawler (); $ts->perform ()?>