Spring boot + Jsoup 搭建高清视频解析系统接口只需1分钟

点击上方“码农突围”,马上关注

这里是码农充电第一站,回复“666”,获取一份专属大礼包

真爱,请设置“星标”或点个“在看”

作者: 绘你一世倾城

来源:https://dwz.cn/4gurLbiC

本文属原创投稿,作者wst,首发于公众号:java版web项目

前几天有个小姐姐想看《天使陷落》的大片问有没有资源的渠道。本着不给广大程序小老哥的脸,想也没想的就答应给小姐姐找资源。但是凄惨的是把以前经常用的网站烦了个遍都没有。

这点小问题肯定难不到勤奋的小老弟,一通谷歌、百度、dogedoge终于找了可靠资源。

既然现在资源这么匮乏,网站都找到了何不试试抓包看看接口。说干就干,按照一贯的操作流程,打开Chrome->开发者工具->NextWork->All

经过一顿猛操作终于于发现接口,其实就是通过调用https://xxx/jx.php?do=h5&id=视频唯一标识来获取到原视频播放地址。

到这一步的时候,已经是觉得都成了,可是打开Postman跑接口一看,人都傻了。

根本就没有返回视频播放源的地址,当然对于爬虫分析都得有耐心,像这种通过Postman跑出来的数据和浏览器控制台抓的数据不一样,大几率就是header参数缺失的问题,知道问题出在哪里问题就迎刃而解。然后一个一个参数的copy过来果不其然少了一个referer参数。

接口参数抓到了就开始拿到我们需要的video路径。我们请求接口的参数返回的是html代码块。

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
        <meta name="referrer" content="never">
        <title>电影盒子</title>
        <link rel="stylesheet" href="https://cdn.bootcss.com/dplayer/1.22.2/DPlayer.min.css">
        <style type="text/css">
body, html, .dplayer {
      padding: 0;
      margin: 0;
      width: 100%;
      height: 100%;
      background-color:#000;
    }
a {
    text-decoration: none;
  }
</style>
    </head>
    <body>
        <div id="player" class="dplayer"></div>
        <script src="https://cdn.bootcss.com/flv.js/1.4.2/flv.min.js"></script>
        <script src="https://cdn.bootcss.com/dplayer/1.22.2/DPlayer.min.js"></script>
        <script type="text/javascript">
  var isiPad = navigator.userAgent.match(/iPad|iPhone|Android|Linux|iPod/i) != null;
  if(isiPad){
  document.getElementById('player').innerHTML = '
            <video src="http://sh-download.weiyun.com/ftn_handler/d77c3b67df5795dcbdd3a69d3cf3503753d6559d01114d3904e741eb27d266c9/%E7%83%88%E7%81%AB%E8%8B%B1%E9%9B%842019.mp4" controls="controls" preload="preload" poster="http://jx.178du.com/Dplayer/loading.gif" width="100%" height="100%" x-webkit-airplay="allow"></video>';
      }else {
    var pic = "";
    var dplayer = new DPlayer({
      element: document.getElementById("player"),
      autoplay: true,
            hotkey: true,
            preload: 'auto',
      video: {
        url: 'http://sh-download.weiyun.com/ftn_handler/d77c3b67df5795dcbdd3a69d3cf3503753d6559d01114d3904e741eb27d266c9/%E7%83%88%E7%81%AB%E8%8B%B1%E9%9B%842019.mp4',
                                pic: 'http://jx.178du.com/Dplayer/loading.png'
             }
    });
       }
        </script>
        <script>
var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?71034aa175241a731523ac24bdd3406d";
  var s = document.getElementsByTagName("script")[0];
  s.parentNode.insertBefore(hm, s);
})();
</script>
    </body>
</html>

这个时候我们可以通过正则从下面代码块里,拿到video标签里面的src值。

<video src="http://sh-download.weiyun.com/ftn_handler/d77c3b67df5795dcbdd3a69d3cf3503753d6559d01114d3904e741eb27d266c9/%E7%83%88%E7%81%AB%E8%8B%B1%E9%9B%842019.mp4" controls="controls" preload="preload" poster="http://jx.178du.com/Dplayer/loading.gif" width="100%" height="100%" x-webkit-airplay="allow"></video>

说干就干,打开强大的IEDA,新建Spring boot项目。在Maven配置pom.xml加入Jsoup解析插件。

<dependency>
     <groupId>org.jsoup</groupId>
        <artifactId>jsoup</artifactId>
        <version>1.8.1</version>
</dependency>

先通过Jsoup调用之前抓取到的接口解析得到Html。

/**
    * https://www.fenggoudy.com
    *
     * @Param null
    * @Author:tianminghai
    * @Date:5:28 PM 2019/11/15
    */
    public static String getVideoUrlForFengGou(String apiUrl,String refeurl)throws Exception{

        // 参数详情 https://www.fenggoudy.com 采集
        apiUrl = "https://jx33.178du.com/jx.php?do=h5&id=7aa7f493692edb4a.mp4";
        refeurl = "https://www.fenggoudy.com";

        Document html = null;

        try {
            html = Jsoup.connect(apiUrl).header("referer",refeurl).get();


        } catch (IOException e) {
            throw e;
        }

        String videoUrl = getHtmlVideoStr(html.outerHtml()).get("src");

        System.out.print("------原始数据:"+getHtmlVideoStr(html.outerHtml()));

        if (videoUrl.contains("download.weiyun.com")){
            return videoUrl;
        }else {
            throw new Exception("数据解析问题");
        }

    }

具体通过正则表达式解析网页得到video中的src值方法。

/**
    *得到html中的 video src
    *
     * @Param null
    * @Author:tianminghai
    * @Date:5:25 PM 2019/11/15
    */
    public static Map<String, String> getHtmlVideoStr(String htmlStr) {

        Map<String, String> pics = new HashMap<String, String>();
        String regEx_video="<video.*src\\s*=\\s*(.*?)[^>]*?>";
        Pattern p = Pattern.compile(regEx_video,Pattern.CASE_INSENSITIVE);
        Matcher m = p.matcher(htmlStr);
        String video="";
        Map<String, String> maps = new HashMap<String, String>();
        while (m.find()) {
            video=m.group();
            Matcher mPoster = Pattern.compile("poster\\s*=\\s*\"?(.*?)(\"|>|\\s+)").matcher(video);
            Matcher mSrc = Pattern.compile("src\\s*=\\s*\"?(.*?)(\"|>|\\s+)").matcher(video);
            String poster ="";
            String src="";
            while (mPoster.find()){
                poster=mPoster.group(1);
            }

            while (mSrc.find()){
                src=mSrc.group(1);
            }
            maps.put("poster", poster);
            maps.put("src", src);
            maps.put("srcs", src);
        }

        return maps;

    }

这个时候一切都准备就绪就差把数据通过接口来实现了,新建一个Controller。

@RestController
public class VideoController {

    @RequestMapping(value = "/video")
    public ResultModel test(){

        
        ResultModel resultModel = new ResultModel();

        resultModel.setMsg("成功");

        try {
            resultModel.setCode(JsonConfigModel.SUSSESS);

            JSONObject obj = new JSONObject();

            obj.put("url",VideoUtils.getVideoUrlForFengGou("",""));

            resultModel.setObj(obj);

        } catch (Exception e) {
            resultModel.setCode(JsonConfigModel.ERROR_VIDEO);
        }
        

        return resultModel;
    }
}

小程序访问接口看看到底能不能观看视频。新建一个小程序项目,添加一个视频播放组件。Vide.WXML;

<cu-custom bgColor="bg-main" isBack="{{true}}">
    <view slot="backText">返回</view>
    <view slot="content">列表</view>
  </cu-custom>


<view class="p tc">
  <video id="myVideo" src="{{voideUrl}}" enable-danmu danmu-btn controls>
  </video>
</view>

Video.js

var http = require('../../utils/httputils.js');

Page({
  data: {
    voideUrl:''
    
  },

  onLoad: function (options) {

    var that = this;

    console.log("-------")
    var prams = {
      username: "1111",
      password: "123456"
    }
    http.getRequest("http://192.168.0.103:8081/video", prams,
      function (res) {
        console.log("----aa---"+res.obj.url)
        that.setData({
          voideUrl:res.obj.url,
        })
      },
      function (err) {

      })
    
  },

  onReady() {
   
  }

  
});

万事具备,来给我动作整齐划一的走起!

那么问题来了,今天下午小姐姐约我去吃饭,老哥们能否指点招?

说句题外话,有不少人想加鱼哥微信,鱼哥姑且放出来,但是坑位有限哦

有热门推荐????

1、阿里巴巴Java 程序员常用的 10 款开源工具!用好了,事半功倍!

2、年底再现暴力裁员!患病员工被关小黑屋,摄像头监控,工作量超其他人!

3、花了三个月终于把所有的 Python 库全部整理了!可以说很全面了

4、工作9年程序员去华为面试要一万月薪,被淘汰后才发现:要少了!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值