<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    午后星期午

    通過JAVA獲取優(yōu)酷、土豆、酷6、6間房等視頻

    通過JAVA獲取優(yōu)酷、土豆、酷6、6間房、56視頻,現(xiàn)在很多社會(huì)網(wǎng)站都有這個(gè)功能,用戶輸入優(yōu)酷、土豆、酷6、6間房、56視頻地址后,能找到對應(yīng)的視頻及視頻的縮略圖,有些社區(qū)網(wǎng)站還能獲取到視頻的時(shí)長。
    比如:新浪微博就有這個(gè)功能,當(dāng)用戶輸入優(yōu)酷、土豆、酷6、6間房、56視頻網(wǎng)址后,就能獲取到相應(yīng)的視頻地址及視頻的縮略圖。

    代碼如下:
    import org.jsoup.Jsoup;
    import org.jsoup.nodes.Document;
    import org.jsoup.nodes.Element;
    import org.jsoup.select.Elements;

    public class VideoUtil {
        
        
    /**
         * 獲取視頻信息
         * 
    @param url
         * 
    @return
         
    */
        
    public static Video getVideoInfo(String url){
            Video video 
    = new Video();
            
            
    if(url.indexOf("v.youku.com")!=-1){
                
    try {
                    video 
    = getYouKuVideo(url);
                } 
    catch (Exception e) {
                    video 
    = null;
                }
            }
    else if(url.indexOf("tudou.com")!=-1){
                
    try {
                    video 
    = getTudouVideo(url);
                } 
    catch (Exception e) {
                    video 
    = null;
                }
            }
    else if(url.indexOf("v.ku6.com")!=-1){
                
    try {
                    video 
    = getKu6Video(url);
                } 
    catch (Exception e) {
                    video 
    = null;
                }
            }
    else if(url.indexOf("6.cn")!=-1){
                
    try {
                    video 
    = get6Video(url);
                } 
    catch (Exception e) {
                    video 
    = null;
                }
            }
    else if(url.indexOf("56.com")!=-1){
                
    try {
                    video 
    = get56Video(url);
                } 
    catch (Exception e) {
                    video 
    = null;
                }
            }
            
            
    return video;
        }
        
        
        
    /**
         * 獲取優(yōu)酷視頻
         * 
    @param url  視頻URL
         
    */
        
    public static Video getYouKuVideo(String url) throws Exception{
            Document doc 
    = getURLContent(url);
            
            
    /**
             *獲取視頻縮略圖 
             
    */
            String pic 
    = getElementAttrById(doc, "s_sina""href");
            
    int local = pic.indexOf("pic=");
            pic 
    = pic.substring(local+4);
            
            
    /**
             * 獲取視頻地址
             
    */        
            String flash 
    = getElementAttrById(doc, "link2""value");
            
            
    /**
             * 獲取視頻時(shí)間
             
    */    
            String time 
    = getElementAttrById(doc, "download""href");
            String []arrays 
    = time.split("\\|");
            time 
    = arrays[4];
            
            Video video 
    = new Video();
            video.setPic(pic);
            video.setFlash(flash);
            video.setTime(time);
            
            
    return video;
        }
        
        
        
    /**
         * 獲取土豆視頻
         * 
    @param url  視頻URL
         
    */
        
    public static Video getTudouVideo(String url) throws Exception{
            Document doc 
    = getURLContent(url);
            String content 
    = doc.html();
            
    int beginLocal = content.indexOf("<script>document.domain");
            
    int endLocal = content.indexOf("</script>");
            content 
    = content.substring(beginLocal, endLocal);
            
            
    /**
             * 獲取視頻地址
             
    */    
            String flash 
    = getScriptVarByName("iid_code", content);
            flash 
    = "http://www.tudou.com/v/" + flash + "/v.swf";
            
            
    /**
             *獲取視頻縮略圖 
             
    */
            String pic 
    = getScriptVarByName("thumbnail", content);
            
            
    /**
             * 獲取視頻時(shí)間
             
    */    
            String time 
    = getScriptVarByName("time", content);

            Video video 
    = new Video();
            video.setPic(pic);
            video.setFlash(flash);
            video.setTime(time);
            
            
    return video;
        }
        
        
        
    /**
         * 獲取酷6視頻
         * 
    @param url  視頻URL
         
    */
        
    public static Video getKu6Video(String url) throws Exception{
            Document doc 
    = getURLContent(url);
            
            
    /**
             * 獲取視頻地址
             
    */
            Element flashEt 
    = doc.getElementById("outSideSwfCode");
            String flash 
    = flashEt.attr("value");
            
            
    /**
             * 獲取視頻縮略圖
             
    */
            Element picEt 
    = doc.getElementById("plVideosList");
            String time 
    = null;
            String pic 
    = null;
            
    if(picEt!=null){
                Elements pics 
    = picEt.getElementsByTag("img");
                pic 
    = pics.get(0).attr("src");
                
                
    /**
                 * 獲取視頻時(shí)長
                 
    */
                Element timeEt 
    = picEt.select("span.review>cite").first(); 
                time 
    = timeEt.text();
            }
    else{
                pic 
    = doc.getElementsByClass("s_pic").first().text();
            }
            
            Video video 
    = new Video();
            video.setPic(pic);
            video.setFlash(flash);
            video.setTime(time);
            
            
    return video;
            
        }
        
        
        
    /**
         * 獲取6間房視頻
         * 
    @param url  視頻URL
         
    */
        
    public static Video get6Video(String url) throws Exception{
            Document doc 
    = getURLContent(url);
            
            
    /**
             * 獲取視頻縮略圖
             
    */
            Element picEt 
    = doc.getElementsByClass("summary").first();
            String pic 
    = picEt.getElementsByTag("img").first().attr("src");
            
            
    /**
             * 獲取視頻時(shí)長
             
    */
            String time 
    = getVideoTime(doc, url, "watchUserVideo");
            
    if(time==null){
                time 
    = getVideoTime(doc, url, "watchRelVideo");
            }
            
            
    /**
             * 獲取視頻地址
             
    */
            Element flashEt 
    = doc.getElementById("video-share-code");
            doc 
    = Jsoup.parse(flashEt.attr("value"));  
            String flash 
    = doc.select("embed").attr("src");
            
            Video video 
    = new Video();
            video.setPic(pic);
            video.setFlash(flash);
            video.setTime(time);
            
            
    return video;
        }
        
        
        
    /**
         * 獲取56視頻
         * 
    @param url  視頻URL
         
    */
        
    public static Video get56Video(String url) throws Exception{
            Document doc 
    = getURLContent(url);
            String content 
    = doc.html();
            
            
    /**
             * 獲取視頻縮略圖
             
    */
            
    int begin = content.indexOf("\"img\":\"");
            content = content.substring(begin+7, begin+200);
            
    int end = content.indexOf("\"};");
            String pic = content.substring(0, end).trim();
            pic 
    = pic.replaceAll("\\\\""");        
            
            
    /**
             * 獲取視頻地址
             
    */
            String flash 
    = "http://player.56.com" + url.substring(url.lastIndexOf("/"), url.lastIndexOf(".html")) + ".swf";
            
            Video video 
    = new Video();
            video.setPic(pic);
            video.setFlash(flash);
            
            
    return video;
        }

        
    /**
         * 獲取6間房視頻時(shí)長    
         
    */
        
    private static String getVideoTime(Document doc, String url, String id) {
            String time 
    = null;
            
            Element timeEt 
    = doc.getElementById(id); 
            Elements links 
    = timeEt.select("dt > a");
            
            
            
    for (Element link : links) {
              String linkHref 
    = link.attr("href");
              
    if(linkHref.equalsIgnoreCase(url)){
                  time 
    = link.parent().getElementsByTag("em").first().text();
                  
    break;
              }
            }
            
    return time;
        }
        
                
        
    /**
         * 獲取script某個(gè)變量的值
         * 
    @param name  變量名稱
         * 
    @return   返回獲取的值 
         
    */
        
    private static String getScriptVarByName(String name, String content){
            String script 
    = content;
            
            
    int begin = script.indexOf(name);
            
            script 
    = script.substring(begin+name.length()+2);
            
            
    int end = script.indexOf(",");
            
            script 
    = script.substring(0,end);
            
            String result
    =script.replaceAll("'""");
            result 
    = result.trim();
            
            
    return result;
        }
        
        
        
    /**
         * 根據(jù)HTML的ID鍵及屬于名,獲取屬于值
         * 
    @param id  HTML的ID鍵
         * 
    @param attrName  屬于名
         * 
    @return  返回屬性值
         
    */
        
    private static String getElementAttrById(Document doc, String id, String attrName)throws Exception{
            Element et 
    = doc.getElementById(id);
            String attrValue 
    = et.attr(attrName);
            
            
    return attrValue;
        }
        
        
        
        
    /**
         * 獲取網(wǎng)頁的內(nèi)容
         
    */
        
    private static Document getURLContent(String url) throws Exception{
            Document doc 
    = Jsoup.connect(url)
              .data(
    "query""Java")
              .userAgent(
    "Mozilla")
              .cookie(
    "auth""token")
              .timeout(
    6000)
              .post();
            
    return doc;
        }
        
        
        
    public static void main(String[] args) {
            
    //String url = "http://v.youku.com/v_show/id_XMjU0MjI2NzY0.html";
            
    //String url = "http://www.tudou.com/programs/view/pVploWOtCQM/";
            
    //String url = "http://v.ku6.com/special/show_4024167/9t7p64bisV2A31Hz.html";
            
    //String url = "http://v.ku6.com/show/BpP5LeyVwvikbT1F.html";
            
    //String url = "http://6.cn/watch/14757577.html";
            String url = "http://www.56.com/u64/v_NTkzMDEzMTc.html";
            Video video 
    = getVideoInfo(url);
            System.out.println(
    "視頻縮略圖:"+video.getPic());
            System.out.println(
    "視頻地址:"+video.getFlash());
            System.out.println(
    "視頻時(shí)長:"+video.getTime());
        }
    }

    視頻封裝類

    public class Video {
        
    private String flash;
        
    private String pic;
        
    private String time;
        
    public String getFlash() {
            
    return flash;
        }
        
    public void setFlash(String flash) {
            
    this.flash = flash;
        }
        
    public String getPic() {
            
    return pic;
        }
        
    public void setPic(String pic) {
            
    this.pic = pic;
        }
        
    public String getTime() {
            
    return time;
        }
        
    public void setTime(String time) {
            
    this.time = time;
        }
    }

    其中要用到一個(gè)JAR包,鏈接地址如下:
    http://jsoup.org/packages/jsoup-1.5.2.jar

    posted on 2013-12-31 13:59 午后星期午 閱讀(89) 評論(0)  編輯  收藏 所屬分類: JavaEE

    主站蜘蛛池模板: 美国毛片亚洲社区在线观看| 精品多毛少妇人妻AV免费久久| 中文字幕亚洲乱码熟女一区二区| 在线观看视频免费完整版| a视频在线免费观看| 免费人成网上在线观看| 亚洲高清毛片一区二区| 久久综合亚洲色一区二区三区| 国产精品亚洲mnbav网站 | 四虎影在线永久免费四虎地址8848aa | 亚洲av日韩av天堂影片精品| www.91亚洲| 免费观看的毛片大全| 亚洲精品免费观看| 永久免费av无码网站yy| a级毛片免费观看在线| 国产成人亚洲综合无| 亚洲AV永久无码天堂影院 | 91精品国产免费久久国语蜜臀| 中文在线日本免费永久18近| 免费精品国产自产拍在线观看| 亚洲精品无码久久久久YW| 亚洲AV无码国产精品色| 亚洲国产美女精品久久久久∴| 又粗又硬免费毛片| 国产精品免费电影| 国产真人无遮挡作爱免费视频 | 亚洲视频一区二区三区四区| 亚洲色偷偷偷网站色偷一区| 日本久久久久亚洲中字幕| 亚洲AV无码专区在线播放中文 | 大地资源中文在线观看免费版| 久久国产美女免费观看精品| 一级毛片**免费看试看20分钟| 美女免费精品高清毛片在线视| 精品久久久久久久久亚洲偷窥女厕| 亚洲砖码砖专无区2023| 亚洲日韩精品无码专区| 亚洲AV成人无码网站| 在线观看亚洲专区| 男女污污污超污视频免费在线看|