php爬虫实战(抓取美拍视频)

  1. 抓取页面

    成都创新互联公司长期为上千余家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为陕西企业提供专业的成都做网站、网站设计,陕西网站改版等技术服务。拥有十余年丰富建站经验和众多成功案例,为您定制开发。

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

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

然后我们会获取到整个页面的代码,接下来就是从代码中提取出视频的地址 标题 图片等关键信息

2.提取

  我们发现视频的主要代码集中在以下代码中


    
    
        
                                               哈喇嘎子流成河              
                                                         小优Lucky         

                                  3060         
                                  100              

通过正则匹配

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

        preg_match_all($pat, $page, $matches, PREG_PATTERN_ORDER);
        for ($i=0; $i (.*?)<\/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;
    }
}

全部代码

.*?<\/li>/ism";

        preg_match_all($pat, $page, $matches, PREG_PATTERN_ORDER);
        for ($i=0; $i (.*?)<\/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);
?>

最后结果:

array(24) {
  [0]=>
  array(3) {
    ["voide"]=>
    string(51) "http://mvvideo2.meitudata.com/5737fd5caeb838981.mp4"
    ["title"]=>
    string(27) "老师那些年常说的话"
    ["img"]=>
    string(58) "/upload/otherpic60/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) "女明星之间的内心戏"
    ["img"]=>
    string(58) "/upload/otherpic60/108722.jpg!thumb320"
  }
  [3]=>
  array(3) {
    ["voide"]=>
    string(51) "http://mvvideo2.meitudata.com/5737eb9d0bfc92046.mp4"
    ["title"]=>
    string(24) "真替老师感到悲剧"
    ["img"]=>
    string(57) "/upload/otherpic60/108723.jpg!thumb320"
  }

接下来。。。你可以存入数据库


本文标题:php爬虫实战(抓取美拍视频)
网页地址:http://pwwzsj.com/article/jccdjg.html

其他资讯