public class VideoUtil { /** * 通过URL获取视频html源代码 * @author 赵新民 * @param url 视频地址 * @param encoding 字符集编码 * @return 返回html源代码 */ public static String getHtmlResultByUrl(String url,String encoding){ StringBuffer sb=new StringBuffer(); InputStreamReader isr=null; try { //建立网络连接 URL urlobj=new URL(url); //打开网络连接 URLConnection uc=urlobj.openConnection(); //创建文件输入流 isr=new InputStreamReader(uc.getInputStream(), encoding); System.out.println("打印输入流为:"+isr); //建立文件缓冲写入流 BufferedReader br=new BufferedReader(isr); String temp=null; while((temp=br.readLine())!=null){ sb.append(temp+"\n"); } } catch (MalformedURLException e) { // TODO Auto-generated catch block System.out.println("链接网络失败!"); e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block System.out.println("打开链接失败!"); e.printStackTrace(); }finally{ if(isr!=null){ try { isr.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } return sb.toString(); } /** * 解析优酷的源代码 * @author 赵新民 * @return HashMap*/ public static HashMap getVideo(String html){ HashMap map=new HashMap (); //使用jsoup组件解析通过url地址获取到的html Document doc = Jsoup.parse(html); //获取id="handle_share" 节点 Element element = doc.getElementById("panel_share"); //获取class="item" 节点 Elements elements = element.getElementsByClass("item"); int i=1; //遍历获取指定节点 for (Element ele : elements) { Element e = ele.getElementById("link"+i); if(e!=null){ map.put("link"+i, e.val()); } i++; } return map; }
java使用Jsoup解析html
(2)条精彩评论。