java 获取优酷视频(转载)

 

  1. import java.io.IOException;  
  2. import java.io.UnsupportedEncodingException;  
  3. import java.net.MalformedURLException;  
  4.   
  5. import org.jsoup.Jsoup;  
  6. import org.jsoup.nodes.Document;  
  7. import org.jsoup.nodes.Element;  
  8.   
  9.   
  10. /** 
  11. * 获取优酷视频 
  12. * @author sunlightcs 
  13. * 2011-3-29 
  14. * http://hi.juziku.com/sunlightcs/ 
  15. */  
  16. public class VideoTest {  
  17.   
  18.     public static void main(String[] args) throws Exception{  
  19.         String pic = getElementAttrById("s_sina""href");  
  20.         int local = pic.indexOf("pic=");  
  21.         pic = pic.substring(local+4);  
  22.         System.out.println("视频缩略图:"+pic);  
  23.           
  24.           
  25.         String flashUrl = getElementAttrById("link2""value");  
  26.         System.out.println("视频地址:"+flashUrl);  
  27.           
  28.           
  29.         String time = getElementAttrById("download""href");  
  30.         String []arrays = time.split("//|");  
  31.         time = arrays[4];  
  32.         System.out.println("视频时长:"+time);  
  33.           
  34.     }  
  35.       
  36.       
  37.     /** 
  38.      * 根据HTML的ID键及属于名,获取属于值 
  39.      * @param id  HTML的ID键 
  40.      * @param attrName  属于名 
  41.      * @return  返回属性值 
  42.      */  
  43.     private static String getElementAttrById(String id, String attrName)throws Exception{  
  44.         Document doc = getURLContent();  
  45.         Element et = doc.getElementById(id);  
  46.         String attrValue = et.attr(attrName);  
  47.           
  48.         return attrValue;  
  49.     }  
  50.       
  51.   
  52.       
  53.     /** 
  54.      * 获取优酷网页的内容 
  55.      */  
  56.     private static Document getURLContent() throws MalformedURLException, IOException, UnsupportedEncodingException {  
  57.         Document doc = Jsoup.connect("http://v.youku.com/v_show/id_XMjU0MjI2NzY0.html")  
  58.           .data("query""Java")  
  59.           .userAgent("Mozilla")  
  60.           .cookie("auth""token")  
  61.           .timeout(3000)  
  62.           .post();  
  63.         return doc;  
  64.     }  
  65.   
  66. }  

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值