获取优酷、土豆、酷6、6间房等视频

通过JAVA获取优酷、土豆、酷6、6间房、56视频,现在很多社会网站都有这个功能,用户输入优酷、土豆、酷6、6间房、56视频地址后,能找到对应的视频及视频的缩略图,有些社区网站还能获取到视频的时长。
比如:新浪微博就有这个功能,当用户输入优酷、土豆、酷6、6间房、56视频网址后,就能获取到相应的视频地址及视频的缩略图。

其中要用到一个JAR包,链接地址如下:
http://jsoup.org/packages/jsoup-1.5.2.jar

原文地址:http://www.juziku.com/wiki/906.htm 

核心代码:

Java代码    收藏代码
  1. import org.jsoup.Jsoup;  
  2. import org.jsoup.nodes.Document;  
  3. import org.jsoup.nodes.Element;  
  4. import org.jsoup.select.Elements;  
  5.   
  6. /** 
  7. * 视频工具类 
  8. * http://hi.juziku.com/sunlightcs/ 
  9. */  
  10. public class VideoUtil {  
  11.       
  12.     /** 
  13.      * 获取视频信息 
  14.      * @param url 
  15.      * @return 
  16.      */  
  17.     public static Video getVideoInfo(String url){  
  18.         Video video = new Video();  
  19.           
  20.         if(url.indexOf("v.youku.com")!=-1){  
  21.             try {  
  22.                 video = getYouKuVideo(url);  
  23.             } catch (Exception e) {  
  24.                 video = null;  
  25.             }  
  26.         }else if(url.indexOf("tudou.com")!=-1){  
  27.             try {  
  28.                 video = getTudouVideo(url);  
  29.             } catch (Exception e) {  
  30.                 video = null;  
  31.             }  
  32.         }else if(url.indexOf("v.ku6.com")!=-1){  
  33.             try {  
  34.                 video = getKu6Video(url);  
  35.             } catch (Exception e) {  
  36.                 video = null;  
  37.             }  
  38.         }else if(url.indexOf("6.cn")!=-1){  
  39.             try {  
  40.                 video = get6Video(url);  
  41.             } catch (Exception e) {  
  42.                 video = null;  
  43.             }  
  44.         }else if(url.indexOf("56.com")!=-1){  
  45.             try {  
  46.                 video = get56Video(url);  
  47.             } catch (Exception e) {  
  48.                 video = null;  
  49.             }  
  50.         }  
  51.           
  52.         return video;  
  53.     }  
  54.       
  55.       
  56.     /** 
  57.      * 获取优酷视频 
  58.      * @param url  视频URL 
  59.      */  
  60.     public static Video getYouKuVideo(String url) throws Exception{  
  61.         Document doc = getURLContent(url);  
  62.           
  63.         /** 
  64.          *获取视频缩略图  
  65.          */  
  66.         String pic = getElementAttrById(doc, "s_sina""href");  
  67.         int local = pic.indexOf("pic=");  
  68.         pic = pic.substring(local+4);  
  69.           
  70.         /** 
  71.          * 获取视频地址 
  72.          */       
  73.         String flash = getElementAttrById(doc, "link2""value");  
  74.           
  75.         /** 
  76.          * 获取视频时间 
  77.          */   
  78.         String time = getElementAttrById(doc, "download""href");  
  79.         String []arrays = time.split("\\|");  
  80.         time = arrays[4];  
  81.           
  82.         Video video = new Video();  
  83.         video.setPic(pic);  
  84.         video.setFlash(flash);  
  85.         video.setTime(time);  
  86.           
  87.         return video;  
  88.     }  
  89.       
  90.       
  91.     /** 
  92.      * 获取土豆视频 
  93.      * @param url  视频URL 
  94.      */  
  95.     public static Video getTudouVideo(String url) throws Exception{  
  96.         Document doc = getURLContent(url);  
  97.         String content = doc.html();  
  98.         int beginLocal = content.indexOf("<script>document.domain");  
  99.         int endLocal = content.indexOf("</script>");  
  100.         content = content.substring(beginLocal, endLocal);  
  101.           
  102.         /** 
  103.          * 获取视频地址 
  104.          */   
  105.         String flash = getScriptVarByName("iid_code", content);  
  106.         flash = "http://www.tudou.com/v/" + flash + "/v.swf";  
  107.           
  108.         /** 
  109.          *获取视频缩略图  
  110.          */  
  111.         String pic = getScriptVarByName("thumbnail", content);  
  112.           
  113.         /** 
  114.          * 获取视频时间 
  115.          */   
  116.         String time = getScriptVarByName("time", content);  
  117.   
  118.         Video video = new Video();  
  119.         video.setPic(pic);  
  120.         video.setFlash(flash);  
  121.         video.setTime(time);  
  122.           
  123.         return video;  
  124.     }  
  125.       
  126.       
  127.     /** 
  128.      * 获取酷6视频 
  129.      * @param url  视频URL 
  130.      */  
  131.     public static Video getKu6Video(String url) throws Exception{  
  132.         Document doc = getURLContent(url);  
  133.           
  134.         /** 
  135.          * 获取视频地址 
  136.          */  
  137.         Element flashEt = doc.getElementById("outSideSwfCode");  
  138.         String flash = flashEt.attr("value");  
  139.           
  140.         /** 
  141.          * 获取视频缩略图 
  142.          */  
  143.         Element picEt = doc.getElementById("plVideosList");  
  144.         String time = null;  
  145.         String pic = null;  
  146.         if(picEt!=null){  
  147.             Elements pics = picEt.getElementsByTag("img");  
  148.             pic = pics.get(0).attr("src");  
  149.               
  150.             /** 
  151.              * 获取视频时长 
  152.              */  
  153.             Element timeEt = picEt.select("span.review>cite").first();   
  154.             time = timeEt.text();  
  155.         }else{  
  156.             pic = doc.getElementsByClass("s_pic").first().text();  
  157.         }  
  158.           
  159.         Video video = new Video();  
  160.         video.setPic(pic);  
  161.         video.setFlash(flash);  
  162.         video.setTime(time);  
  163.           
  164.         return video;  
  165.           
  166.     }  
  167.       
  168.       
  169.     /** 
  170.      * 获取6间房视频 
  171.      * @param url  视频URL 
  172.      */  
  173.     public static Video get6Video(String url) throws Exception{  
  174.         Document doc = getURLContent(url);  
  175.           
  176.         /** 
  177.          * 获取视频缩略图 
  178.          */  
  179.         Element picEt = doc.getElementsByClass("summary").first();  
  180.         String pic = picEt.getElementsByTag("img").first().attr("src");  
  181.           
  182.         /** 
  183.          * 获取视频时长 
  184.          */  
  185.         String time = getVideoTime(doc, url, "watchUserVideo");  
  186.         if(time==null){  
  187.             time = getVideoTime(doc, url, "watchRelVideo");  
  188.         }  
  189.           
  190.         /** 
  191.          * 获取视频地址 
  192.          */  
  193.         Element flashEt = doc.getElementById("video-share-code");  
  194.         doc = Jsoup.parse(flashEt.attr("value"));    
  195.         String flash = doc.select("embed").attr("src");  
  196.           
  197.         Video video = new Video();  
  198.         video.setPic(pic);  
  199.         video.setFlash(flash);  
  200.         video.setTime(time);  
  201.           
  202.         return video;  
  203.     }  
  204.       
  205.       
  206.     /** 
  207.      * 获取56视频 
  208.      * @param url  视频URL 
  209.      */  
  210.     public static Video get56Video(String url) throws Exception{  
  211.         Document doc = getURLContent(url);  
  212.         String content = doc.html();  
  213.           
  214.         /** 
  215.          * 获取视频缩略图 
  216.          */  
  217.         int begin = content.indexOf("\"img\":\"");  
  218.         content = content.substring(begin+7, begin+200);  
  219.         int end = content.indexOf("\"};");  
  220.         String pic = content.substring(0, end).trim();  
  221.         pic = pic.replaceAll("\\\\", "");         
  222.           
  223.         /** 
  224.          * 获取视频地址 
  225.          */  
  226.         String flash = "http://player.56.com" + url.substring(url.lastIndexOf("/"), url.lastIndexOf(".html")) + ".swf";  
  227.           
  228.         Video video = new Video();  
  229.         video.setPic(pic);  
  230.         video.setFlash(flash);  
  231.           
  232.         return video;  
  233.     }  
  234.   
  235.     /** 
  236.      * 获取6间房视频时长     
  237.      */  
  238.     private static String getVideoTime(Document doc, String url, String id) {  
  239.         String time = null;  
  240.           
  241.         Element timeEt = doc.getElementById(id);   
  242.         Elements links = timeEt.select("dt > a");  
  243.           
  244.           
  245.         for (Element link : links) {  
  246.           String linkHref = link.attr("href");  
  247.           if(linkHref.equalsIgnoreCase(url)){  
  248.               time = link.parent().getElementsByTag("em").first().text();  
  249.               break;  
  250.           }  
  251.         }  
  252.         return time;  
  253.     }  
  254.       
  255.               
  256.     /** 
  257.      * 获取script某个变量的值 
  258.      * @param name  变量名称 
  259.      * @return   返回获取的值  
  260.      */  
  261.     private static String getScriptVarByName(String name, String content){  
  262.         String script = content;  
  263.           
  264.         int begin = script.indexOf(name);  
  265.           
  266.         script = script.substring(begin+name.length()+2);  
  267.           
  268.         int end = script.indexOf(",");  
  269.           
  270.         script = script.substring(0,end);  
  271.           
  272.         String result=script.replaceAll("'""");  
  273.         result = result.trim();  
  274.           
  275.         return result;  
  276.     }  
  277.       
  278.       
  279.     /** 
  280.      * 根据HTML的ID键及属于名,获取属于值 
  281.      * @param id  HTML的ID键 
  282.      * @param attrName  属于名 
  283.      * @return  返回属性值 
  284.      */  
  285.     private static String getElementAttrById(Document doc, String id, String attrName)throws Exception{  
  286.         Element et = doc.getElementById(id);  
  287.         String attrValue = et.attr(attrName);  
  288.           
  289.         return attrValue;  
  290.     }  
  291.       
  292.       
  293.       
  294.     /** 
  295.      * 获取网页的内容 
  296.      */  
  297.     private static Document getURLContent(String url) throws Exception{  
  298.         Document doc = Jsoup.connect(url)  
  299.           .data("query""Java")  
  300.           .userAgent("Mozilla")  
  301.           .cookie("auth""token")  
  302.           .timeout(6000)  
  303.           .post();  
  304.         return doc;  
  305.     }  
  306.       
  307.       
  308.     public static void main(String[] args) {  
  309.         //String url = "http://v.youku.com/v_show/id_XMjU0MjI2NzY0.html";  
  310.         //String url = "http://www.tudou.com/programs/view/pVploWOtCQM/";  
  311.         //String url = "http://v.ku6.com/special/show_4024167/9t7p64bisV2A31Hz.html";  
  312.         //String url = "http://v.ku6.com/show/BpP5LeyVwvikbT1F.html";  
  313.         //String url = "http://6.cn/watch/14757577.html";  
  314.         String url = "http://www.56.com/u64/v_NTkzMDEzMTc.html";  
  315.         Video video = getVideoInfo(url);  
  316.         System.out.println("视频缩略图:"+video.getPic());  
  317.         System.out.println("视频地址:"+video.getFlash());  
  318.         System.out.println("视频时长:"+video.getTime());  
  319.     }  
  320. }  

 

视频封装类:

Java代码    收藏代码
  1. /** 
  2. * 视频封装 
  3. * http://hi.juziku.com/sunlightcs/ 
  4. */  
  5. public class Video {  
  6.     private String flash;  
  7.     private String pic;  
  8.     private String time;  
  9.     public String getFlash() {  
  10.         return flash;  
  11.     }  
  12.     public void setFlash(String flash) {  
  13.         this.flash = flash;  
  14.     }  
  15.     public String getPic() {  
  16.         return pic;  
  17.     }  
  18.     public void setPic(String pic) {  
  19.         this.pic = pic;  
  20.     }  
  21.     public String getTime() {  
  22.         return time;  
  23.     }  
  24.     public void setTime(String time) {  
  25.         this.time = time;  
  26.     }  
  27. }  
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值