Heim  >  Artikel  >  Backend-Entwicklung  >  PHP-Crawler erfasst Songtexte

PHP-Crawler erfasst Songtexte

不言
不言Original
2018-04-16 09:33:511429Durchsuche

Der Inhalt dieses Artikels handelt von PHP-Crawler-Crawling-Texten, die einen gewissen Referenzwert haben. Jetzt können Freunde in Not darauf verweisen

<?php
header("Content-type:text/html;charset=utf-8");
class Small_crawler
{
    protected $regular;       //获取内容正则
    protected $url;           //源头
    protected $i;             //控制数量
    protected $count;         //总数
    protected $lyrics_ze;     //歌词内容
    protected $lyrics_lrc;    //歌词地址匹配
    protected $txt;           //存储地址


    function __construct()
    {
        $this->regular = "/<a href=\"\/song\/([0-9])*\".*>*<\/a>/";

        $this->lyrics_ze = "/<p id=\"lyricCont\".*<\/p>/";

        $this->lyrics_lrc = "/\"http:\/\/.*\"/";

        $this->url = &#39;http://music.baidu.com/&#39;;

        $this->txt = "playlist.txt";

        $this->i = 0;

        $this->count = 110;

    }


    /**
     * 开始任务
     */
    function perform()
    {
        $data = file_get_contents($this->url);

        if(preg_match_all($this->regular, $data, $mar)){

            $this->lyrics($mar); //歌词筛选

        }else{

            echo &#39;已没有可用信息&#39;;
            exit;

        }

    }


    /**
     * @param $data
     * 歌词处理
     */
    function lyrics($data)
    {

        if(is_array($data)){

            //歌词筛选
            foreach ($data[0] as $k=>$y){

                $data_s = explode(&#39;"&#39;,$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(&#39;"&#39;,$lyrics_c[0][0]);

                            $lyrics_file = file_get_contents($lyrics_c[1]);

                            $this->write($lyrics_file);

                        }
                    }

                }

            }

            //循环操作
            foreach ($data[0] as $ki=>$yi){

                $data_t = explode(&#39;"&#39;,$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); //歌词筛选
                    }

                }
            }

        }

    }


    /**
     * @param $lyrics_file
     * 写入文件
     */
    function write($lyrics_file)
    {

        if($this->i < $this->count){

            $acs = preg_replace(&#39;/(\[.*\])/&#39;,&#39;&#39;,$lyrics_file);

            $myfile = fopen($this->txt, "a");
            fwrite($myfile,$acs);
            fwrite($myfile,"\r\n".$this->i.".-----------------------------------------------------------\r\n");

            $this->i++;
        }else{

            exit; //结束停止
        }

    }

}

//运行
$ts = new Small_crawler();
$ts->perform()

?>

Verwandte Empfehlungen:

Verwenden Sie den PHP-Crawler, um die Immobilienpreise in Nanjing zu analysieren

PHP-Crawler erfasst Baidu Tieba-Bilder



Das obige ist der detaillierte Inhalt vonPHP-Crawler erfasst Songtexte. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Vorheriger Artikel:PHP-Imagick-MaskenmethodeNächster Artikel:PHP-Imagick-Maskenmethode