由于想在微信公众号里爬一点儿考研的视频

花了差不多一天的时间把这个爬虫做好(其实也不算爬虫吧,就算个能批量处理的地址解析器,半个爬虫)

不多说,进正题

(本文适合有java基础的同学,没基础的用客户端缓存然后格式转换吧)

所需条件:

1.一台联网的有java环境的电脑

2.耐心

访问后台接口网址:

http://vv.video.qq.com/getinfo(低清的只要这一个就好了)

http://vv.video.qq.com/getkey(高清的需要访问这个)

原理(获取低清视频,先把原理打通,高清后期有时间会更):

步骤一:

获取你想要下载的视频的腾讯视频页面地址(这个很容易啦,就不赘述)

此处以:https://v.qq.com/x/page/f08302y6rof.html为例

步骤二:

获取视频vid

此处的vid为f08302y6rof,就是上边儿网址上那一串长长的东西

步骤三:

用获取到的视频的vid替换下面红色标明的vid(这一步是访问后台接口得到json报文)

http://vv.video.qq.com/getinfo?vids=f08302y6rof&platform=101001&charge=0&otype=json&defn=shd

然后访问

步骤四:

在页面返回浏览器的json报文中,找到fn 和 fvkey

我这边儿传回的报文如下

  1. QZOutputJson={"dltype":1,"exem":0,"fl":{"cnt":2,"fi":[{"id":100701,"name":"msd","lmt":0,"sb":1,"cname":"标清;(270P)","br":29,"profile":2,"drm":0,"video":1,"audio":1,"fs":101567331,"super":0,"hdr10enh":0,"sl":1},{"id":2,"name":"mp4","lmt":0,"sb":1,"cname":"高清;(480P)","br":34,"profile":1,"drm":0,"video":1,"audio":1,"fs":130427092,"super":0,"hdr10enh":0,"sl":0}]},"hs":0,"ip":"111.79.225.65","ls":0,"preview":3383,"s":"o","sfl":{"cnt":0},"tm":1556431150,"vl":{"cnt":1,"vi":[{"br":29,"ch":0,"cl":{"fc":0,"keyid":"f08302y6rof.100701"},"ct":21600,"drm":0,"dsb":0,"fmd5":"74e3040ce70af50716abead16c9fba50","fn":"f08302y6rof.m701.mp4","fs":101567331,"fst":5,"fvkey":"D351DB69FA6EC791CB6DE47266F80B21BFFFAA3616A7B42975903ED5EA68589C0E2454137002A84799CF43B4FD972B415259C1F23C21CD34F2C34BC64F6D7D16F21BF3BF94F22B09491FC9D8C96CFFA3B3177345807F34EFDDAF94449E72FC3B8C55751EE9EADC5F","head":0,"hevc":0,"iflag":0,"level":0,"lnk":"f08302y6rof","logo":1,"mst":8,"pl":null,"share":1,"sp":0,"st":2,"tail":0,"td":"3383.47","ti":"2020考研数学寒假计划(第一次课)","tie":0,"type":3,"ul":{"ui":[{"url":"http://ugcws.video.gtimg.com/uwMROfz0r5zAoaQXGdGnC2dfhzlOR5XW60pRw41PvMP8tDlH/","vt":106,"dtc":0,"dt":2},{"url":"http://ugcydzd.qq.com/uwMROfz0r5zAoaQXGdGlC2dfhznfaJdqBNmJ_NLSRfZb0kLT/","vt":146,"dtc":0,"dt":2},{"url":"http://ugcsjy.qq.com/uwMROfz0r5zAoaQXGdGlK2dfhzmm-mdByiC0fycrmmUBpCVq/","vt":176,"dtc":0,"dt":2},{"url":"http://video.dispatch.tc.qq.com/uwMROfz0r5zAoaQXGdGlLGdfhzn3bYHHUWfJ-3lk8pLFnjzb/","vt":0,"dtc":0,"dt":2}]},"vh":480,"vid":"f08302y6rof","videotype":0,"vr":0,"vst":2,"vw":270,"wh":0.5625,"wl":{"wi":[{"id":19,"x":14,"y":14,"w":84,"h":27,"a":100,"md5":"dcc9dc5c478c4100ea2817c5e6020f26","url":"http://puui.qpic.cn/vcolumn_pic/0/logo_qing_xi_color_336_108.png/0","surl":"https://puui.qpic.cn/vcolumn_pic/0/logo_qing_xi_color_336_108.png/0"}]},"uptime":1548118095,"fvideo":0,"fvpint":0,"swhdcp":0}]}};

(传回来的就是一行,我直接用java代码解析了,手动找费眼睛)

步骤五:

利用获取到的fn和fvkey构建视频下载地址

此处构建的是:

http://ugcws.video.gtimg.com/f08302y6rof.m701.mp4?vkey=2E657DF01414A1F95E0B3CF7F187CEB84B3E439F5D0BA2D7F052967654DEFDE53292F0BE8BCD373FA0F269BA6BE5CC1AD5CC4AEE269AB0B1C72261815608260190B1D14D9B1820B0394DAB0C8DA1D8561F3B3455FBE5BA27D618C81A0A233256DDDAB6429E3A05FF

把获取到的fn替换前边儿一个短的标红内容

fvkey替换后边儿长的标红内容

这就是完整的视频下载地址了,可以用迅雷下载

完成

源码如下(有错误或者不规范请大佬指出,个人机器上可以运行):

  1. package catchVedio;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.BufferedWriter;
  5. import java.io.File;
  6. import java.io.FileReader;
  7. import java.io.FileWriter;
  8. import java.io.IOException;
  9. import java.io.InputStreamReader;
  10. import java.io.UnsupportedEncodingException;
  11. import java.net.HttpURLConnection;
  12. import java.net.URL;
  13. import java.util.ArrayList;
  14. import java.util.List;
  15.  
  16. /**
  17. * 获取视频接口的json
  18. * @author Administrator
  19. *
  20. */
  21. public class CatchVedio {
  22. // Socket client = new Scoket();
  23. private URL url;
  24. private HttpURLConnection urlConnection;
  25. private int responseCode;
  26. private BufferedReader reader;
  27. private BufferedWriter writer;
  28.  
  29. public static void main(String[] args) {
  30. CatchVedio cv = new CatchVedio();
  31. try {
  32.  
  33. String[] VedioURL = cv.get_VedioURL();//接收
  34. for(String temp:VedioURL) {//temp是每一个视频的播放地址
  35. cv.toDownloadURL(cv.analyse(cv.get_Json(temp)));//写出到文件
  36. }
  37. } catch (IOException e) {
  38. // TODO 自动生成的 catch 块
  39. e.printStackTrace();
  40. }finally {
  41. try {
  42. cv.reader.close();
  43. cv.writer.close();
  44. } catch (IOException e) {
  45. // TODO 自动生成的 catch 块
  46. e.printStackTrace();
  47. }
  48. }
  49.  
  50. }
  51.  
  52. void toDownloadURL(String real_url) throws IOException {//将对应下载地址输出到文件
  53. this.writer = new BufferedWriter(new FileWriter("D:/worm/downloadURL.txt",true));//定义追加方式写入的流
  54. // this.writer.append(real_url);
  55. this.writer.write(real_url+"\r\n");
  56. this.writer.flush();
  57. }
  58.  
  59. String analyse(String json) {//分析json,传回完整下载地址
  60. int fvkey_index = json.indexOf("\"fvkey\":\"")+9;
  61. int endIndex = json.indexOf("\"",fvkey_index);
  62. String fvkey = json.substring(fvkey_index,endIndex);//获取到fvkey
  63. // System.out.println(fvkey);
  64.  
  65. int fn_index = json.indexOf("\"fn\":\"")+6;
  66. int fn_end = json.indexOf("\"",fn_index);
  67. String fn = json.substring(fn_index,fn_end);//获取到视频文件名
  68. // System.out.println(fn);
  69.  
  70. String head = "http://ugcws.video.gtimg.com/";
  71.  
  72. StringBuffer real_url = new StringBuffer();
  73. real_url.append(head);//加入头部
  74. real_url.append(fn+"?");//加入文件名
  75. real_url.append("vkey="+fvkey);//加入解锁码
  76. /*构造成功*/
  77. // System.out.println(real_url.toString());
  78. return real_url.toString();
  79.  
  80. }
  81.  
  82. String get_Json(String url) throws UnsupportedEncodingException, IOException {
  83. String line = "";
  84. StringBuffer sb = new StringBuffer();
  85. this.url = new URL(url);
  86. this.urlConnection = (HttpURLConnection)this.url.openConnection();
  87. this.responseCode = this.urlConnection.getResponseCode();
  88. if (this.responseCode == 200) {
  89. this.reader = new BufferedReader(new InputStreamReader(this.urlConnection.getInputStream(), "UTF-8"));
  90. while ((line = this.reader.readLine()) != null) {
  91. sb.append(line);// 网页传回的只有一行
  92. }
  93. return sb.toString();
  94. }
  95. return "";
  96. }
  97.  
  98. String[] get_VedioURL() throws IOException {
  99. // void get_VedioURL() throws IOException {
  100. File file = new File("D:/worm/vedioURL.txt");
  101. String line = "";
  102. this.reader = new BufferedReader(new FileReader(file));
  103. String[] t = new String[0];
  104. List<String> container = new ArrayList<String>();
  105. while(null!=(line = this.reader.readLine())) {
  106. if(line.equals("")) {
  107. continue;
  108. }
  109. line = this.change(line);//转换一下
  110. container.add(line);//装入容器
  111. }
  112. return container.toArray(t);
  113. }
  114. /**
  115. * http://vv.video.qq.com/getinfo?vids=x0164ytbgov&platform=101001&charge=0&otype=json&defn=shd //格式
  116. * @param str
  117. * @return
  118. * https://v.qq.com/x/page/f08302y6rof.html//页面地址示例
  119. * https://v.qq.com/x/page/y083158hphd.html
  120. * https://v.qq.com/x/page/c08503oe58c.html
  121. */
  122. String change(String str) {//定义从页面播放地址获取vid转换到后台接口地址的方法
  123. String head = "http://vv.video.qq.com/getinfo?vids=";
  124. String tail = "&platform=101001&charge=0&otype=json&defn=shd";
  125. String vid = str.substring(str.indexOf("page/")+5,str.indexOf(".html"));
  126. return head+vid+tail;
  127. }
  128. }

我是输入输出都是文件操作

希望对大家有所帮助

以上

java_爬虫_从腾讯视频播放界面爬取视频真实地址的更多相关文章

  1. 爬虫概念与编程学习之如何爬取视频网站页面(用HttpClient)(二)

    先看,前一期博客,理清好思路. 爬虫概念与编程学习之如何爬取网页源代码(一) 不多说,直接上代码. 编写代码 运行 <!DOCTYPE html><html><head& ...

  2. 爬虫入门(三)——动态网页爬取:爬取pexel上的图片

    Pexel上有大量精美的图片,没事总想看看有什么好看的自己保存到电脑里可能会很有用 但是一个一个保存当然太麻烦了 所以不如我们写个爬虫吧(๑•̀ㅂ•́)و✧ 一开始学习爬虫的时候希望爬取pexel上的 ...

  3. 爬虫系列(六) 用urllib和re爬取百度贴吧

    这篇文章我们将使用 urllib 和 re 模块爬取百度贴吧,并使用三种文件格式存储数据,下面先贴上最终的效果图 1.网页分析 (1)准备工作 首先我们使用 Chrome 浏览器打开 百度贴吧,在输入 ...

  4. 爬虫系列(十) 用requests和xpath爬取豆瓣电影

    这篇文章我们将使用 requests 和 xpath 爬取豆瓣电影 Top250,下面先贴上最终的效果图: 1.网页分析 (1)分析 URL 规律 我们首先使用 Chrome 浏览器打开 豆瓣电影 T ...

  5. 爬虫系列(十一) 用requests和xpath爬取豆瓣电影评论

    这篇文章,我们继续利用 requests 和 xpath 爬取豆瓣电影的短评,下面还是先贴上效果图: 1.网页分析 (1)翻页 我们还是使用 Chrome 浏览器打开豆瓣电影中某一部电影的评论进行分析 ...

  6. Python爬虫入门教程:豆瓣Top电影爬取

        基本开发环境 Python 3.6 Pycharm 相关模块的使用 requests parsel csv 安装Python并添加到环境变量,pip安装需要的相关模块即可. 爬虫基本思路 一. ...

  7. java_爬虫_获取经过js渲染后的网页源码

    md 弄了一天了……(这个月不会在摸爬虫了,浪费生命) 进入正题: 起初是想写一个爬虫来爬一个网站的视频,但是怎么爬取都爬取不到,分析了下源代码之后,发现源代码中并没有视频的dom 但是在浏览器检查元 ...

  8. Pyhton网络爬虫实例_豆瓣电影排行榜_BeautifulSoup4方法爬取

    -----------------------------------------------------------学无止境------------------------------------- ...

  9. Python爬虫入门教程 2-100 妹子图网站爬取

    妹子图网站爬取---前言 从今天开始就要撸起袖子,直接写Python爬虫了,学习语言最好的办法就是有目的的进行,所以,接下来我将用10+篇的博客,写爬图片这一件事情.希望可以做好. 为了写好爬虫,我们 ...

随机推荐

  1. Faster R-CNN: Towards Real-Time Object Detection with Region Proposal Networks(理解)

    0 - 背景 R-CNN中检测步骤分成很多步骤,fast-RCNN便基于此进行改进,将region proposals的特征提取融合成共享卷积层问题,但是,fast-RCNN仍然采用了selectiv ...

  2. LeetCode前100题(EASY难度)

    1 Two Sum Given an array of integers, return indices of the two numbers such that they add up to a s ...

  3. TP5报错

    Array to string conversion 数组不能用echo来输出,可使用var_dump().dump()或print_r()

  4. Linux日志每日备份脚本

    2018-5-28 10:59:07 星期一 原理是: 1. 每天0点0分crontab执行备份脚本 2. 先将当前日志文件copy一份作为备份, 备份文件名的后缀为前一天 3. 用当前日志的最后50 ...

  5. 【原创】大数据基础之Marathon(1)简介、安装、使用

    marathon 1.6.322 官方:https://mesosphere.github.io/marathon/ 一 简介 Marathon is a production-grade conta ...

  6. cocos2dx-lua 文件操作

    print("开始") --检查文件是否存在 local path = "ABC.txt" local fileExist = cc.FileUtils:get ...

  7. linux日志过滤某时间段的日志

    sed -n '/2019-01-10 16:00*/,/2019-01-10 18:48*/p' nohup.out > 111.log

  8. URL.createObjectURL() 实现本地上传图片 并预览功能

    URL.createObjectURL() 静态方法会创建一个 DOMString,其中包含一个表示参数中给出的对象的URL.这个 URL 的生命周期和创建它的窗口中的 document 绑定.这个新 ...

  9. CAP分布式事务 学习及简单demo

    完全参考 github的指导 demo地址, Pub使用 efcore , Sub 使用 dapper, mysql数据库 https://files.cnblogs.com/files/xtxtx/ ...

  10. [转]IntelliJ IDEA 使用spring-boot-devtools热部署无效解决办法

    来源:https://www.jianshu.com/p/4d8aa6dfd103 相信大部分使用IntelliJ IDEA的同学都会遇到这个问题,即使项目使用了spring-boot-devtool ...