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

3 篇文章 0 订阅
我的博客传送门(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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值