抖音数据采集

<?php
/**
* 抖音链接url
*/
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;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 自己做dede的开发时间也比较长了,基本上常用的函数都知道在哪个文件里面,但是时间一长,也有点模糊了,俗话说:好记...
    大刘的英语世界阅读 5,773评论 1 9
  • "use strict";function _classCallCheck(e,t){if(!(e instanc...
    久些阅读 6,202评论 0 2
  • 1. 如何数年如一日的早起? 思想:1)习惯/仪式吸引我们行事,而不是意志和纪律。习惯的力量可以确保我们在不是非常...
    EdwardMa阅读 1,751评论 0 0
  • 早晨一直跑到泰山脚下,去年爬过了泰山,这次没有计划。越跑越自豪,我这是在用双脚丈量世界啊。 很奇怪的事 如果走一公...
    史终成长阅读 2,118评论 0 0
  • 冻僵的两旁树 伸展着干干静静带着疤痕的胳膊 四仰八叉朝着天 整整齐齐排列着设计好的故事 压抑的黑褐色 爬上白茫茫的...
    shuiwusenlin阅读 1,742评论 2 5

友情链接更多精彩内容