欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

php采集好看短视频,写入hexo博客

程序员文章站 2022-05-26 14:34:16
...
我的博客传送门(https://lzz1.top)查看效果

<?php
/**
 * Created by PhpStorm.
 * User: Lzz
 * Date: 2019/2/28
 * Time: 10:03
 */
class Haokan {
    # 配置
    protected $option = [
        'url' => 'https://sv.baidu.com/',
        'header' => [
            'referer: https://sv.baidu.com/',
            'user-agent: Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_1 like Mac OS X) AppleWebKit/603.1.30 (KHTML, like Gecko) Version/10.0 Mobile/14E304 Safari/602.1'
        ],
    ];
    # dom
    protected $xp;
    # 分类
    protected $fenlei=[];
    # 获取到的视频链接
    protected $vsrc=[];

    public function __construct($option=[])
    {
        $this->option = array_merge($this->option,$option);
    }
    # 获取分类
    public function get_fenlei(){
        $list = $this->c_dom($this->option['url'])->xp->query('//div[@class="video-tabs-wrapper"]/ul/li/a');
        for($i=0;$i<$list->length;$i++){
            array_push($this->fenlei,[
                'href' => $list->item($i)->getAttribute('href'),
                'name' => $list->item($i)->textContent
            ]);
        }
        return $this;
    }
    # 获取分类下的短视频链接
    public function get_active_sp(){
        $this->get_fenlei();
        $t = time();
        $tt = (time()+1800).'000';
        foreach ($this->fenlei as $k=>$f){
            $href = str_replace('#','',$f['href']);
            $url = "https://sv.baidu.com/videoui/list/tab?source=wise-channel&pd=&subTab=$href&caller=bdwise";
            $list = $this->http_curl($url);
            preg_match_all('/(?<=data-vsrc=).*?.mp4/',$list,$arr);
            $this->vsrc[$k] = [
                'href' => $f['href'],
                'name' => $f['name'],
                'list' => [

                ]
            ];
            foreach ($arr[0] as $a){
                if(strlen($a)>255){
                    unset($a);
                }
                array_push($this->vsrc[$k]['list'],str_replace('\"','',$a));
            }
        }
        return $this->vsrc;
    }
    # 这里你们不需要了
    # 创建hexo文章
    public function create_hexo(){
        $this->get_active_sp();
        $title = '好看短视频'.date('Ymd');
        $date = date('Y-m-d H:i:s');
        $md = <<<MDTITLE
title: $title
entitle: ''
author: lzz
avatar: /images/favicon.ico
authorLink: 'https://lzz1.top'
authorAbout: 'https://lzz1.top'
authorDesc: "另一个搬\U0001F9F1的劳斯基\U0001F601️️"
categories: 采集
date: $date
tags:
    - 采集
keywords: 采集
description: 好看短视频采集
photos:
---

MDTITLE;
        foreach ($this->vsrc as $v){
            $md .= <<<MDCONTENT
### {$v['name']}\n
MDCONTENT;
            foreach ($v['list'] as $kk=>$vv){
                if(!empty($vv)){
                    $md .= <<<MDLIST
> <video style="width:50%;max-height:240px" controls><source src="$vv" type="video/mp4"/></video>\n
MDLIST;
                }
            }

        }
        file_put_contents('source/_posts/'.$title.'.md',$md);
    }

    # 创建Dom
    public function c_dom($url){
        $dom = new DOMDocument('1.0','utf-8');
        $html = $this->http_curl($url);
        @$dom->loadHTML($html);
        $dom->normalize();
        $xp = new DOMXPath($dom);
        $this->xp = $xp;
        return $this;
    }
    #
    public function http_curl($url){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);//要访问的url
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//https网站取消ssl验证
        curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);//允许30*跳转
        curl_setopt($ch, CURLOPT_TIMEOUT,30);//设置超时时间
        curl_setopt($ch,CURLOPT_HTTPHEADER,$this->option['header']);
        $response = curl_exec($ch);
        if ($response === false) {
            $error_info =  curl_error($ch);
            echo $error_info;
            curl_close($ch);//关闭curl
            return false;
        } else {
            //echo curl_getinfo($ch, CURLINFO_HEADER_OUT);
            curl_close($ch);//关闭curl
            return $response;
        }
    }
}
$haokan = new Haokan();
$haokan->create_hexo();

直接本地 打开命令行,到根目录  运行  php haokan.php