用 Java 抓取优酷、土豆等视频

1. [代码][JavaScript]代码  
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
 
/**
* 视频工具类
* @author sunlightcs
* 2011-4-6
* http://hi.juziku.com/sunlightcs/
*/
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;
        }
         
         
        /**
         * 获取优酷视频
         * @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");
                 
                /**
                 * 获取视频时间
                 */    
                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("");
                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);
                 
                /**
                 * 获取视频时间
                 */    
                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");
                         
                        /**
                         * 获取视频时长
                         */
                        Element timeEt = picEt.select("span.review>cite").first(); 
                        time = timeEt.text();手绘图片
                }else{http://www.bizhizu.cn/shouhui/
                        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");
                 
                /**
                 * 获取视频时长
                 */
                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间房视频时长    
         */
        private static String getVideoTime(Document doc, String url, String id) {
                String time = null;
                 
                Element timeEt = doc.getElementById(id); 

转载于:https://www.cnblogs.com/xkzy/p/3916530.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值