抖音数据采集

作者: 杏花_我不想活了 | 来源:发表于2019-04-17 13:33 被阅读5次

<?php
private function douyin ($url){

   $number_maps = [
        0 => ['&#xe603;', '&#xe60d;', '&#xe616;'],
        1 => ['&#xe602;', '&#xe60e;', '&#xe618;'],
        2 => ['&#xe605;', '&#xe610;', '&#xe617;'],
        3 => ['&#xe604;', '&#xe611;', '&#xe61a;'],
        4 => ['&#xe606;', '&#xe60c;', '&#xe619;'],
        5 => ['&#xe607;', '&#xe60f;', '&#xe61b;'],
        6 => ['&#xe608;', '&#xe612;', '&#xe61f;'],
        7 => ['&#xe613;', '&#xe60a;', '&#xe61c;'],
        8 => ['&#xe614;', '&#xe60b;', '&#xe61d;'],
        9 => ['&#xe609;', '&#xe61e;', '&#xe615;'],
    ];

    $opts = [
        'http' => [
            'header' => "User-Agent: Mozilla/5.0 (Linux; Android 5.0; SM-G900P Build/LRX21T) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Mobile Safari/537.36",
        ]
    ];
    $context = stream_context_create($opts);
    $html = file_get_contents($url, false, $context);
    if (!$html) {
        return [];
    }

    $data = [];
    // 封面图
    preg_match('/id="videoPoster"\s*style="background-image:url\((.*?)\)">/i', $html, $match);
    $data['cover_url'] = $match[1];

    //抖音昵称
    preg_match('/class="user-info-name">\@(.*?)<\//i', $html, $match);
    $data['douyin_name'] = $match[1];

    //抖音ID
    preg_match('/<\s*p\s*class="user-info-id">.*?ID:(.*?)<\/p>/i', $html, $match);
    $data['douyin_uid'] = str_replace(' ', '', strip_tags($match[1]));

    //点赞数
    preg_match('/<\s*div\s*class="info-item info-like"\s*.*?>.*?<\s*p\s*class="count"\s*>(.*?)<\/p>/i', $html, $match);
    $data['thumb_times'] = str_replace(' ', '', strip_tags($match[1]));

    // 评论数
    preg_match('/<\s*div.*data-item="comment"\s*>.*?<\s*p\s*class="count"\s*>(.*?)<\/p>/i', $html, $match);
    $data['comment_times'] = str_replace(' ', '', strip_tags($match[1]));

    // $data_str = json_encode($data);
    foreach ($number_maps as $k => $v) {
        $data = str_replace($v, $k, $data);
    }

    $thumb_times_unit = strtolower(substr($data['thumb_times'], -1));
    if ($thumb_times_unit == 'w') {
        $data['thumb_times'] = intval(floatval($data['thumb_times']) * 10000);
    } else if ($thumb_times_unit == 'k') {
        $data['thumb_times'] = intval(floatval($data['thumb_times']) * 1000);
    }

    $comment_times_unit = strtolower(substr($data['comment_times'], -1));
    if ($comment_times_unit == 'w') {
        $data['comment_times'] = intval(floatval($data['comment_times']) * 10000);
    } else if ($comment_times_unit == 'k') {
        $data['comment_times'] = intval(floatval($data['comment_times']) * 1000);
    }

    preg_match('/itemId\s*:\s*"(\d+)"/i', $html, $match);
    $data['douyin_item_id'] = $match[1];

    preg_match('/uid:\s*"(\d+)"/i', $html, $match);
    $user_id = $match[1];
    $html = file_get_contents('http://www.iesdouyin.com/share/user/' . $user_id, false, $context);
    $data['user_id'] = $user_id;

    //粉丝数
    preg_match('/<\s*span\s*class="follower block"\s*.*?>.*?<\s*span\s*class="num"\s*.*?>(.*?)<\/span>/i', $html, $match);
    $data['fans_times'] = str_replace(' ', '', strip_tags($match[1]));

    //关注数
    preg_match('/<\s*span\s*class="liked-num block"\s*.*?>.*?<\s*span\s*class="num"\s*.*?>(.*?)<\/span>/i', $html, $match);
    $data['like_times'] = str_replace(' ', '', strip_tags($match[1]));

    foreach ($number_maps as $k => $v) {
         $data = str_replace($v, $k, $data);
    }

    $fans_times_unit = strtolower(substr($data['fans_times'], -1));
    if ($fans_times_unit == 'w') {
        $data['fans_times'] = intval(floatval($data['fans_times']) * 10000);
    } else if ($fans_times_unit == 'k') {
        $data['fans_times'] = intval(floatval($data['fans_times']) * 1000);
    }

    $like_times_unit = strtolower(substr($data['like_times'], -1));
    if ($like_times_unit == 'w') {
       $data['like_times'] = intval(floatval($data['like_times']) * 10000);
    } else if ($like_times_unit == 'k') {
        $data['like_times'] = intval(floatval($data['like_times']) * 1000);
    }

    return $data;
}

相关文章

网友评论

    本文标题:抖音数据采集

    本文链接:https://www.haomeiwen.com/subject/kczgwqtx.html