- import java.io.IOException;
- import java.io.UnsupportedEncodingException;
- import java.net.MalformedURLException;
- import org.jsoup.Jsoup;
- import org.jsoup.nodes.Document;
- import org.jsoup.nodes.Element;
- /**
- * 获取优酷视频
- * @author sunlightcs
- * 2011-3-29
- * http://hi.juziku.com/sunlightcs/
- */
- public class VideoTest {
- public static void main(String[] args) throws Exception{
- String pic = getElementAttrById("s_sina", "href");
- int local = pic.indexOf("pic=");
- pic = pic.substring(local+4);
- System.out.println("视频缩略图:"+pic);
- String flashUrl = getElementAttrById("link2", "value");
- System.out.println("视频地址:"+flashUrl);
- String time = getElementAttrById("download", "href");
- String []arrays = time.split("//|");
- time = arrays[4];
- System.out.println("视频时长:"+time);
- }
- /**
- * 根据HTML的ID键及属于名,获取属于值
- * @param id HTML的ID键
- * @param attrName 属于名
- * @return 返回属性值
- */
- private static String getElementAttrById(String id, String attrName)throws Exception{
- Document doc = getURLContent();
- Element et = doc.getElementById(id);
- String attrValue = et.attr(attrName);
- return attrValue;
- }
- /**
- * 获取优酷网页的内容
- */
- private static Document getURLContent() throws MalformedURLException, IOException, UnsupportedEncodingException {
- Document doc = Jsoup.connect("http://v.youku.com/v_show/id_XMjU0MjI2NzY0.html")
- .data("query", "Java")
- .userAgent("Mozilla")
- .cookie("auth", "token")
- .timeout(3000)
- .post();
- return doc;
- }
- }