溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

php爬蟲實(shí)戰(zhàn)(抓取美拍視頻)

發(fā)布時(shí)間:2020-06-02 01:33:48 來源:網(wǎng)絡(luò) 閱讀:1364 作者:Bing661129 欄目:web開發(fā)
  1. 抓取頁面

 地址:http://www.meipai.com/medias/hot

public function getContentByFilegetcontents($url) {
        $content = file_get_contents($url);
        return $content;
    }

然后我們會(huì)獲取到整個(gè)頁面的代碼,接下來就是從代碼中提取出視頻的地址 標(biāo)題 圖片等關(guān)鍵信息

2.提取

  我們發(fā)現(xiàn)視頻的主要代碼集中在以下代碼中

<li class="pr no-select loading  J_media_list_item" itemscope itemtype="http://schema.org/VideoObject">
    <img src="https://cache.yisu.com/upload/information/20200310/52/108717.jpg!thumb320" width="300" height="300" class="db pa pai" alt="手撕包菜。包菜撕片裝洗凈備用。熱鍋入油五花肉下鍋煸炒出油,多余的油盛出。放醬油肉上色,盛出。之前的油倒鍋內(nèi),放蒜辣椒炒香,下包菜繼續(xù)翻炒,倒適量醬油老抽五香粉。再下之前炒好的五花肉翻炒,放適量鹽,出鍋前放雞精淋入適量香醋即可非常香啊,超級(jí)下飯。喜歡的點(diǎn)贊奧#美食##家常菜#" itemprop="thumbnail">
    <div id="w517161790" class="content-l-video content-l-media-wrap pr cp" data-id="517161790" data-video="http://mvvideo1.meitudata.com/5734040ae2dec950.mp4">
        <div class="layer-black pa"></div>
        <a hidefocus href="/media/517161790" target="_blank" class="content-l-p pa" title="手撕包菜。包菜撕片裝洗凈備用。熱鍋入油五花肉下鍋煸炒出油,多余的油盛出。放醬油肉上色,盛出。之前的油倒鍋內(nèi),放蒜辣椒炒香,下包菜繼續(xù)翻炒,倒適量醬油老抽五香粉。再下之前炒好的五花肉翻炒,放適量鹽,出鍋前放雞精淋入適量香醋即可非常香啊,超級(jí)下飯。喜歡的點(diǎn)贊奧#美食##家常菜#">
            <meta itemprop="url" content="/media/517161790">
            <i class="icon icon-item-play"></i>
            <strong class="js-convert-emoji" itemprop="description">哈喇嘎子流成河</strong>
        </a>
    </div>
    <div class="pr" itemscope itemtype="http://schema.org/AggregateRating">
        <a hidefocus href="/user/62299474" class="dbl h58">
            <img src="https://cache.yisu.com/upload/information/20200310/52/108718.jpg!thumb60" width="28" height="28" class="avatar m10" title="小優(yōu)Lucky" alt="小優(yōu)Lucky">
        </a>
        <p class="content-name  pa">
            <a hidefocus href="/user/62299474" class="content-name-a js-convert-emoji" title="小優(yōu)Lucky" itemprop="author">小優(yōu)Lucky</a>
        </p>
        <div class="content-like pa" data-id="517161790">
            <i class="icon icon-like"></i>
            <span itemprop="ratingCount">3060</span>
        </div>
        <a hidefocus href="/media/517161790" data-sc="1" target="_blank" class="conten-command pa" data-id="517161790">
            <i class="icon icon-command"></i>
            <span itemprop="reviewCount">100</span>
        </a>
    </div>
</li>

通過正則匹配

 public function extracturl($page) {
        $matches = array();
        $voide=array();
        $mainurl="";
        $list=array();
        $j=0;
        $pat = "/<li class=\"pr no-select loading  J_media_list_item\".*?>.*?<\/li>/ism";

        preg_match_all($pat, $page, $matches, PREG_PATTERN_ORDER);
        for ($i=0; $i <count($matches[0]) ; $i++) { 
        	$pat1 ="/data-video=\"(.*?)\"/ism";
	        preg_match_all($pat1, $matches[0][$i], $voide, PREG_PATTERN_ORDER);

	        $myvoide=$voide[1][0];
	        $pat2 ="/src=\"(.*?)\"/ism";
	        preg_match_all($pat2, $matches[0][$i], $img, PREG_PATTERN_ORDER);
	        $myimg=$img[1][0];
	         $pat3 ="/<strong class=\"js-convert-emoji\".*?>(.*?)<\/strong>/ism";
	        preg_match_all($pat3, $matches[0][$i], $title, PREG_PATTERN_ORDER);
	        $mytitle= $title[1][0];
	        $list[$j++]=array(
	        	'voide'=>$myvoide,
	        	'title'=>$mytitle,
	        	'img'=>$myimg);
	        
        }
       	return $list;
    }
}

全部代碼

<?php
class Cutecrawler {

    public function getContentByFilegetcontents($url) {
        $content = file_get_contents($url);
        return $content;
    }
   
    public function extracturl($page) {
        $matches = array();
        $voide=array();
        $mainurl="";
        $list=array();
        $j=0;
        $pat = "/<li class=\"pr no-select loading  J_media_list_item\".*?>.*?<\/li>/ism";

        preg_match_all($pat, $page, $matches, PREG_PATTERN_ORDER);
        for ($i=0; $i <count($matches[0]) ; $i++) { 
        	$pat1 ="/data-video=\"(.*?)\"/ism";
	        preg_match_all($pat1, $matches[0][$i], $voide, PREG_PATTERN_ORDER);

	        $myvoide=$voide[1][0];
	        $pat2 ="/src=\"(.*?)\"/ism";
	        preg_match_all($pat2, $matches[0][$i], $img, PREG_PATTERN_ORDER);
	        $myimg=$img[1][0];
	         $pat3 ="/<strong class=\"js-convert-emoji\".*?>(.*?)<\/strong>/ism";
	        preg_match_all($pat3, $matches[0][$i], $title, PREG_PATTERN_ORDER);
	        $mytitle= $title[1][0];
	        $list[$j++]=array(
	        	'voide'=>$myvoide,
	        	'title'=>$mytitle,
	        	'img'=>$myimg);
	        
        }
       	return $list;
    }
}

$url = "http://www.meipai.com/medias/hot";
	$crawler = new Cutecrawler();
    $content = $crawler->getContentByFilegetcontents($url);
    $c=$crawler->extracturl($content);

var_dump($c);
?>

最后結(jié)果:

array(24) {
  [0]=>
  array(3) {
    ["voide"]=>
    string(51) "http://mvvideo2.meitudata.com/5737fd5caeb838981.mp4"
    ["title"]=>
    string(27) "老師那些年常說的話"
    ["img"]=>
    string(58) "https://cache.yisu.com/upload/information/20200310/52/108720.jpg!thumb320"
  }
  [1]=>
  array(3) {
    ["voide"]=>
    string(50) "http://mvvideo2.meitudata.com/5737fceabf873602.mp4"
    ["title"]=>
    string(21) "女友突然冷落你"
    ["img"]=>
    string(58) "http://mvimg2.meitudata.com/5736d25d0aa5d8991.jpg!thumb320"
  }
  [2]=>
  array(3) {
    ["voide"]=>
    string(51) "http://mvvideo2.meitudata.com/5737f300131e18596.mp4"
    ["title"]=>
    string(27) "女明星之間的內(nèi)心戲"
    ["img"]=>
    string(58) "https://cache.yisu.com/upload/information/20200310/52/108722.jpg!thumb320"
  }
  [3]=>
  array(3) {
    ["voide"]=>
    string(51) "http://mvvideo2.meitudata.com/5737eb9d0bfc92046.mp4"
    ["title"]=>
    string(24) "真替老師感到悲劇"
    ["img"]=>
    string(57) "https://cache.yisu.com/upload/information/20200310/52/108723.jpg!thumb320"
  }

接下來。。。你可以存入數(shù)據(jù)庫

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI