PHP代码实现抖音去水印

PHP代码实现抖音去水印

前言

1、一直在寻找抖音去水印的方法,PHP的要不然过时了,要不然就是java的,或者是Python的。

2、更过分的是有人提供api收费接口打着,开源源码的旗号。

3、这里我就提供纯粹的源码思路,怎么啦!怎么啦

实现思路

1、使用爬虫技术分析、php我们用的querylist

2、在你的框架里面引入 composer jaeger/querylist:"*"

3、分析页面,使用GuzzleHttp请求分析 (GuzzleHttp在querylist 版本4会依赖GuzzleHttp)

页面分析

1、拿到一个抖音分享页面 http://v.douyin.com/xpnQUK/

2、在浏览器里面打开发现他是有路由重定向的我们用代码输出这个页面

$url = 'http://v.douyin.com/xpnQUK/';

$headers = [

    'Connection' => 'keep-alive',

    'Host' => 'aweme.snssdk.com',

    'User-Agent'=>'Mozilla/5.0 (iPhone; CPU iPhone OS 12_1_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/16D57 Version/12.0 Safari/604.1'

];

$client = new Client(['headers'=>$headers]);

//允许重定向获取html

$res = $client->request('GET', $url,['allow_redirects' => true]);

$html = (string)$res->getBody();

echo $html;die;

3、执行上面代码后会发现是一个手机分享页面的输出。

然后我们获取这个短视频的文案标题、视频封面、视频地址

我们重最难的视频地址开始吧

视频地址获取

$Query = QueryList::getInstance();
        $video_src = $Query->html($html)->find('video')->src;

        //爬虫视频地址
        $video_src = str_replace("playwm","play",$video_src);

        //获取url地址之后不能让他重定向
        $res = $client->request('GET', $video_src,['allow_redirects' => false]);
        $data['video_src']= $res->getHeaderLine('location');

视频封面获取

$video_cover = $Query->html($html)->find('.video-poster')->style;
        $video_cover = str_replace("background-image:url(","",$video_cover);
        $data['video_cover'] = trim($video_cover,')');

文案标题获取

$video_title = $Query->html($html)->find('.user-title')->text();
        $title =  explode('@',$video_title);
        for ($i=0;$i<count($title);$i++){
            $video_title = str_replace('@'.$title[$i],"",$video_title);
        }
        $title =  explode('#',$video_title);
        for ($i=0;$i<count($title);$i++){
            $video_title = str_replace('#'.$title[$i],"",$video_title);
        }

        $video_title = str_replace("抖音","",$video_title);
        $data['video_title'] = trim($video_title);

最后代码总结

public function douyin($url){
        $headers = [
            'Connection' => 'keep-alive',
            'Host' => 'aweme.snssdk.com',
            'User-Agent'=>'Mozilla/5.0 (iPhone; CPU iPhone OS 12_1_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/16D57 Version/12.0 Safari/604.1'
        ];
        $client = new Client(['headers'=>$headers]);

        //允许重定向获取html
        $res = $client->request('GET', $url,['allow_redirects' => true]);
        $html = (string)$res->getBody();


        $Query = QueryList::getInstance();
        $video_src = $Query->html($html)->find('video')->src;

        //爬虫视频地址
        $video_src = str_replace("playwm","play",$video_src);

        //获取url地址之后不能让他重定向
        $res = $client->request('GET', $video_src,['allow_redirects' => false]);
        $data['video_src']= $res->getHeaderLine('location');

        //视频封面
        $video_cover = $Query->html($html)->find('.video-poster')->style;
        $video_cover = str_replace("background-image:url(","",$video_cover);
        $data['video_cover'] = trim($video_cover,')');

        $video_title = $Query->html($html)->find('.user-title')->text();
        $title =  explode('@',$video_title);
        for ($i=0;$i<count($title);$i++){
            $video_title = str_replace('@'.$title[$i],"",$video_title);
        }
        $title =  explode('#',$video_title);
        for ($i=0;$i<count($title);$i++){
            $video_title = str_replace('#'.$title[$i],"",$video_title);
        }

        $video_title = str_replace("抖音","",$video_title);
        $data['video_title'] = trim($video_title);
        
        return $data;
    }
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页