2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ニコニコ生放送公式ランキングのスクレイピング

Posted at

概要

PHPSimpleHtmlDomParserを用いたニコニコ生放送公式ランキングのスクレイピング

コード

OfficialRankingScraper.class.php
<?php

require_once("./simple_html_dom.php");

class OfficialRankingScraper
{
    const OFFICIAL_RANKING_URL = "http://live.nicovideo.jp/ranking";
    private $user,$official;
    public function __construct(){

    }

    public function getUser(){
        $this->makeUser();
        return $this->user;
    }

    public function getOfficial(){
        $this->makeOfficial();
        return $this->official;
    }

    private function makeUser(){
        $html = file_get_html(self::OFFICIAL_RANKING_URL);
        $stream_array = array();
        foreach($html->find('div[class=user clearfix spriteIcon ranking_video]') as $i => $element) {
            $rank           = ($i + 1);
            $community      = $element->find('span[class=comName]',0);
            $community_name = $community->plaintext;
            $community_url  = $community->find('a',0)->href;
            $tmp            = explode('/',$community_url);
            $community_id   = $tmp[4];
            $user           = $element->find('span[class=name]',0);
            $user_name      = $user->plaintext;
            $user_url       = $user->find('a',0)->href;
            $viewer         = $element->find('li[class="audience spriteIcon"]',0)->plaintext;
            $comment        = $element->find('li[class="coment spriteIcon"]',0)->plaintext;
            $title          = $element->find('span[class="video_title"]',0)->plaintext;
            $thumbnail      = $element->find('div[class="info"]',0)->find('img',0)->src;
            $stream_url     = $element->find('div[class="info"]',0)->find('a',0)->href;
            $stream_url     = "http://live.nicovideo.jp/".$stream_url;
            $active         = $element->find('span[class="score"]',0)->plaintext;

            $stream_array[] = array(
                'rank'           => $rank,
                'community_id'   => $community_id,
                'community_url'  => $community_url,
                'community_name' => $community_name,
                'user_name'      => $user_name,
                'user_url'       => $user_url,
                'viewer'         => $viewer,
                'comment'        => $comment,
                'title'          => $title,
                'thumbnail'      => $thumbnail,
                'stream_url'     => $stream_url,
                'active'         => $active
            );
        }
        $this->user = $stream_array;
    }

    private function makeOfficial(){
        $html = file_get_html(self::OFFICIAL_RANKING_URL);
        $stream_array = array();
        foreach($html->find('ul[class="active clearfix"] li') as $i => $element) {
            $rank           = ($i + 1);
            $viewer         = $element->find('div[class="audience spriteIcon"]',0)->plaintext;
            $comment        = $element->find('div[class="coment spriteIcon"]',0)->plaintext;
            $title          = $element->find('p a',0)->plaintext;
            $thumbnail      = $element->find('div[class="rankNumber"]',0)->find('img',0)->src;
            $stream_url     = $element->find('div[class="rankNumber"]',0)->find('a',0)->href;
            $stream_url     = "http://live.nicovideo.jp/".$stream_url;
            $stream_id      = $this->makeStreamId($stream_url);

            $stream_array[] = array(
                'rank'           => $rank,
                'viewer'         => $viewer,
                'comment'        => $comment,
                'title'          => $title,
                'thumbnail'      => $thumbnail,
                'stream_url'     => $stream_url,
                'stream_id'      => $stream_id
            );
        }
        $this->official = $stream_array;
    }

    private function makeStreamId($stream_url){
        $stream_id = "";
        $slash     = explode("/", $stream_url);
        $hatena    = explode("?",$slash[4]);
        $stream_id = $hatena[0];
        return $stream_id;
    }


}

?>

サンプルコード

sample.php

<?php
require_once("OfficialRankingScraper.class.php");

$scraper = new OfficialRankingScraper();
print_r($scraper->getOfficial());
print_r($scraper->getUser());


?>

結果

result

Array
(
    [0] => Array
        (
            [rank] => 1
            [viewer] =>  									77,592 								
            [comment] =>  									64,730 								
            [title] => 【衆議院 国会生中継】 ~平成25年3月18日 予算委員会~
            [thumbnail] => http://nl.simg.jp/img/a2/4539.198de4.jpg
            [stream_url] => http://live.nicovideo.jp/watch/lv130208276?ref=rnk_active&zroute=ranking&provider_type=official_and_channel
            [stream_id] => lv130208276
        )

    [1] => Array
        (
            [rank] => 2
            [viewer] =>  									41,713 								
            [comment] =>  									17,362 								
            [title] => 向谷実のニコニコ生レコーディング配信Vol.03(1日目)
            [thumbnail] => http://nl.simg.jp/img/a55/164883.9cec79.jpg
            [stream_url] => http://live.nicovideo.jp/watch/lv129102517?ref=rnk_active&zroute=ranking&provider_type=official_and_channel
            [stream_id] => lv129102517
        )

    [2] => Array
        (
            [rank] => 3
            [viewer] =>  									9,189 								
            [comment] =>  									1,684 								
            [title] => 《暴力問題と助成金不正使用問題で会見》全日本柔道連盟理事会後記者会見 
            [thumbnail] => http://nl.simg.jp/img/a56/165855.ba88d9.jpg
            [stream_url] => http://live.nicovideo.jp/watch/lv130578293?ref=rnk_active&zroute=ranking&provider_type=official_and_channel
            [stream_id] => lv130578293
        )
     ...

)

Array
(
    [0] => Array
        (
            [rank] => 1
            [community_id] => co1406335
            [community_url] => http://com.nicovideo.jp/community/co1406335
            [community_name] => モロちゃんねる(´・ω・`) 
            [user_name] => モロ 
            [user_url] => http://www.nicovideo.jp/user/4770536
            [viewer] => 6,855
            [comment] => 21,927
            [title] => JKが全力で雑談∠(  ∵  )/ 
            [thumbnail] => http://icon.nimg.jp/community/s/140/co1406335.jpg?1362519204
            [stream_url] => http://live.nicovideo.jp/watch/lv130537400?&zroute=ranking&provider_type=community
            [active] => 138 
        )

    [1] => Array
        (
            [rank] => 2
            [community_id] => co1893195
            [community_url] => http://com.nicovideo.jp/community/co1893195
            [community_name] => 金バエリローデッド 
            [user_name] => まこと 
            [user_url] => http://www.nicovideo.jp/user/31151811
            [viewer] => 3,988
            [comment] => 2,250
            [title] => 大雨だからロケは中止して雑談 
            [thumbnail] => http://icon.nimg.jp/community/s/189/co1893195.jpg?1363460057
            [stream_url] => http://live.nicovideo.jp/watch/lv130593299?&zroute=ranking&provider_type=community
            [active] => 61 
        )

    [2] => Array
        (
            [rank] => 3
            [community_id] => co1705340
            [community_url] => http://com.nicovideo.jp/community/co1705340
            [community_name] => 哲学とは哲学であるm9・`ω・)ビシッ!!【命の章】 
            [user_name] => ティロ・フィナーレ加川 放送用 
            [user_url] => http://www.nicovideo.jp/user/28450199
            [viewer] => 12,717
            [comment] => 2,521
            [title] => 初めてのFX 
            [thumbnail] => http://icon.nimg.jp/community/s/170/co1705340.jpg?1363370871
            [stream_url] => http://live.nicovideo.jp/watch/lv130583371?&zroute=ranking&provider_type=community
            [active] => 60 
        )
   ...
)
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?