Through Java to get Youku video, now many social sites have this feature, the user input Youku video address, can find the corresponding video and video thumbnails, some community sites can also get the length of the video.
For example: Sina Weibo has this function, when the user input video URL, you can get the corresponding video address and video thumbnail.
Java.io.unsupportedencodingexception;03import Import Java.io.ioexception;02import java.net.malformedurlexception;04 05import Org.jsoup.jsoup;06import Org.jsoup.nodes.document;07import org.jsoup.nodes.element;08 09 10/**11* get Youku video 12* @author sunlightcs13* 2011-3-2914* http://hi.juziku.com/sunlightcs/ 15*/16public class Videotest {+ public static void main (string[] args) throws exception{19 String pic = Gete Lementattrbyid ("S_sina", "href"); int local = Pic.indexof ("pic="); pic = pic.substring (local+4); 22 System.out.println ("video thumbnail:" +pic); String Flashurl = Getelementattrbyid ("Link2", "value"); System.out.println ("Video address:" +flashurl); String time = Getelementattrbyid ("Download" , "href"); String []arrays = Time.split ("\\|"); Time = arrays[4];32 System.out.println ("Video duration:" +time); 33 34}35 36 37/**38 * Based on The ID key of the HTML and belongs to the name, gets the value 39 *ID key for @param ID HTML * @param attrname belongs to the name of the * @return return attribute value */43 private static String getelementattr Byid (string ID, String attrname) throws exception{44 Document doc = geturlcontent (), Element et = Doc.getel Ementbyid (ID); String attrValue = Et.attr (attrname); attrvalue;49}50 51 52 53/**54 * Get the content of the Youku page */56 private static Document geturlcontent () throws Malformedurlexception, Ioexcepti On, unsupportedencodingexception {Document doc = jsoup.connect ("http://v.youku.com/v_show/id_XMjU0MjI2NzY0.html" ). Data ("Query", "Java") useragent ("Mozilla"), Cookie ("auth", "token"). Timeo UT (+). Post (); Doc;64}65 66}
Jar Package Used
Jsoup-1.5.2.jar