视频分享效果为在站点内输入一视频网站某一视频链接,分享后即可类似微博或qq空间中那样直接点击播放,此代码赞只支持上述视频网站,6间房的视频有点问题,后面将继续扩展抓取其他视频网站。
分享后效果图:
实现代码如下:
package com; /** * 视频实体 * @author hanfei * */ public class Video { private String title; private String desc; 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; } public void setTitle(String title) { this.title = title; } public String getTitle() { return title; } public void setDesc(String desc) { this.desc = desc; } public String getDesc() { return desc; } } 视频工具类: package com; /** * 视频抓取工具类 * @author hanfei * */ 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) { System.out.println(e); video = null; } } else if (url.indexOf("tudou.com") != -1) { try { video = getTudouVideo(url); } catch (Exception e) { System.out.println("视频为空。。。。。"); 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; } } else if (url.indexOf("qiyi.com") != -1) { try { System.out.println("执行奇异网"); video = getQiyiVideo(url); } catch (Exception e) { System.out.println(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="); System.out.println(pic); pic = pic.substring(local + 4); /** * * 获取视频地址 */ String flash = getElementAttrById(doc, "link2", "value"); /** * * 获取视频时间 */ String time = getElementAttrById(doc, "download", "_href"); String[] arrays = time.split("\\|"); System.out.println("time" + time); System.out.println(arrays.length); 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 { // http://www.tudou.com/v/102387574/v.swf Document doc = getURLContent(url); String content = doc.html(); if (url.endsWith(".html")) { System.out.println("执行htmol"); int beginLocal = content.indexOf("<script>var"); int endLocal = content.indexOf("</script>"); content = content.substring(beginLocal, endLocal); // System.out.println(content); // // ScriptEngineManager mgr = new ScriptEngineManager(); // ScriptEngine engine = mgr.getEngineByName("JavaScript"); // engine.eval(content); // Invocable inv = (Invocable) engine; // inv. /** * * 获取视频地址 */ String flash = getScriptVarByName("defaultIid", content); flash = "http://js.tudouui.com/bin/player2/opn_22.swf?iid=" + flash; System.out.print("flashURL" + flash); /** * * 获取视频缩略图 */ 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; } else { int beginLocal = content.indexOf("<script>document.domain"); int endLocal = content.indexOf("</script>"); content = content.substring(beginLocal, endLocal); /** * * 获取视频地址 */ String flash = getScriptVarByName("iid", content); flash = "http://js.tudouui.com/bin/player2/opn_22.swf?iid=" + flash; /** * * 获取视频缩略图 */ 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 { 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; } public static Video getQiyiVideo(String url) throws Exception { Document doc = getURLContent(url); String content = doc.html(); System.out.println("执行htmol"); int beginLocal = content.indexOf("<script"); int endLocal = content.indexOf("</script>"); content = content.substring(beginLocal, endLocal); System.out.println(content); String infoString = content.substring(content.indexOf("{"), content.length()); System.out.println(infoString); JSONObject infoOBJ = JSONObject.fromObject(infoString); String videoId = infoOBJ.getString("videoId"); String title = infoOBJ.getString("title"); // http://www.qiyipic.com/thumb/20111013/v134588.jpg String tvId = infoOBJ.getString("tvId"); String vurl = infoOBJ.getString("url"); vurl = vurl.replace("http://www.", ""); vurl = vurl.replace("http://", ""); // vurl=vurl.replace(":", ""); vurl = vurl.replace("/", "."); vurl = vurl.replace(".", ","); String[] urldata = vurl.split(","); System.out.println(vurl); String image = "http://www.qiyipic.com/thumb/" + urldata[3] + "/v" + tvId + ".jpg"; /** * * 获取视频地址 */ String flashUrl = "http://player.video.qiyi.com/" + videoId + "/0/600/" + urldata[0] + "/" + urldata[3] + "/" + urldata[4] + ".swf"; Video video = new Video(); video.setPic(image); video.setFlash(flashUrl); video.setTime("77"); return video; } /** * * 获取6间房视频时长 */ 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某个变量的值 * * @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; } /** * * 根据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; } /** * * 获取网页的内容 */ 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_XMzExNzA1MDA4.html"; // String url = "http://www.tudou.com/programs/view/CHKM-8D5MmE/"; String url = "http://v.youku.com/v_playlist/f16343935o1p0.html"; // String url = "http://v.ku6.com/show/BpP5LeyVwvikbT1F.html"; // String url = "http://www.tudou.com/playlist/p/l10485826.html"; // String url = "http://www.56.com/u64/v_NTkzMDEzMTc.html"; // String url="http://www.tudou.com/playlist/p/l10485826i60175370.html"; url = "http://www.qiyi.com/dianshiju/20111008/23e2566d0ae56486.html"; // String[] s="yule,qiyi.com.20111011.c1837626ef2280a5.html".split(","); // String a=s[1]; Video video = getVideoInfo(url); if (video == null) { System.out.println(url); } else { System.out.println("视频缩略图:" + video.getPic()); System.out.println("视频地址:" + video.getFlash()); System.out.println("视频时长:" + video.getTime()); } System.out.print(url.endsWith(".html")); } }