Heim  >  Artikel  >  Backend-Entwicklung  >  如何抓取BT天堂电影数据

如何抓取BT天堂电影数据

WBOY
WBOYOriginal
2016-07-30 13:30:522995Durchsuche

晚上休息,想弄两部好看的电影看看,

找了半天没找到想看的如何抓取BT天堂电影数据

想到之前有个人爬知乎的用户数据,突发奇想如何抓取BT天堂电影数据

没事把BT天堂的电影信息爬下来,下次阔以直接查数据库。如何抓取BT天堂电影数据如何抓取BT天堂电影数据

只能说闲的蛋疼如何抓取BT天堂电影数据,哈哈,还能码下代码  ^_^


1.抓取网站html源码

<span style="font-size:24px;">$url = "www.bttiantang.cc";
$html = shell_exec("curl $url");</span>

2.获取总页数,电影总数(正则匹配)

<span style="font-size:24px;">preg_match("/<span class='\"pageinfo\"'>.*?/", $html, $pageCount);
preg_match_all("/\d{1,10000}/",$pageCount[0],$pageCount);</span></span>

3.抓取电影信息(正则匹配信息)

<span style="font-size:24px;">preg_match("/\d{4}\/\d{2}\/\d{2}/" , $pageInfo[0][$i], $updateTime);

preg_match("/<font color='\"#FF6600\"'>(.*?)<i>/" , $pageInfo[0][$i], $movieName);
        
preg_match("/<strong>(\d{1})/" , $pageInfo[0][$i], $movieScore_int);
     
preg_match("/<em class='\"fm\"'>(\d{1})/" , $pageInfo[0][$i], $movieScore_decimal);
        
preg_match("/href=\"(.*?)\"/" , $pageInfo[0][$i], $movieUrl);
      
preg_match("/<p class='\"des\"'>(.*?)/" , $pageInfo[0][$i], $actor);
       </p></em></strong></i></font></span>

4.插入数据库,大功告成

总体来说,php抓取的速度还是蛮快的,4min不到,采集2w多条信息。

start:01:22:54

end:01:26:11



附数据库截图:



附源码:

<?php $url = "www.bttiantang.cc";
$html = shell_exec("curl $url");

preg_match("/<span class=\"pageinfo\">.*?/", $html, $pageCount);
preg_match_all("/\d{1,10000}/",$pageCount[0],$pageCount);

$pageSize = intval($pageCount[0][0]);
$movieCount = $pageCount[0][1];

$conn = mysql_connect('***','***','');
mysql_select_db('***',$conn);
mysql_query('set names utf8',$conn);

for($j=1;$j.*?/s", $movieHtml, $pageInfo);
    for($i=0;$i<count preg_match ad if str_replace color='\"#FF6600\"'>(.*?)<i>/" , $pageInfo[0][$i], $movieName);
        /*****same conditions*****/
        if(empty($movieName))
            preg_match("/<b>(.*?)<i>/" , $pageInfo[0][$i], $movieName);
        if(empty($movieName))
            preg_match("/<b>(.*?)/" , $pageInfo[0][$i], $movieName);
        /************************/
        $movieName = $movieName[1];

        preg_match("/<strong>(\d{1})/" , $pageInfo[0][$i], $movieScore_int);
        $movieScore_int = $movieScore_int[1];
        preg_match("/<em class='\"fm\"'>(\d{1})/" , $pageInfo[0][$i], $movieScore_decimal);
        $movieScore_decimal = $movieScore_decimal[1];
        $movieScore = floatval($movieScore_int.'.'.$movieScore_decimal);

        preg_match("/href=\"(.*?)\"/" , $pageInfo[0][$i], $movieUrl);
        $movieUrl = $movieUrl[1];

        preg_match("/<p class='\"des\"'>(.*?)/" , $pageInfo[0][$i], $actor);
        $movieActor = str_replace("<em>",'',str_replace("</em>",'',$actor[1]));

        mysql_unbuffered_query("insert into movie (name,actor,url,update_ts,score) values ('$movieName','$movieActor','$movieUrl',<span style="white-space:pre">	</span>'$updateTime','$movieScore')");
    }

}

?></p></em></strong></b></i></b></i></count>

本电影信息属于从BT天堂抓取,并未涉及机密信息,因此本人不承担任何法律责任!

若有相关电影信息涉及您的版权或知识产权或其他利益,请告知,确认后会尽快删除。

版权声明:本文为博主原创文章,未经博主允许不得转载。

以上就介绍了如何抓取BT天堂电影数据,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

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