---
title: "通过JAVA获取6间房视频"
author: "sunlightcs"
date: "2011-04-02T16:01:25Z"
tags: [".net", "Flash", "HTML", "Java", "新浪微博"]
categories: ["编程语言"]
slug: "java-6"
draft: false
lastmod: "2011-04-02T16:01:25Z"
id: 984819
url: "https://juziku.iteye.com/blog/984819"
---

通过JAVA获取6间房视频，现在很多社会网站都有这个功能，用户输入6间房视频地址后，能找到对应的视频及视频的缩略图，有些社区网站还能获取到视频的时长。
比如：新浪微博就有这个功能，当用户输入视频网址后，就能获取到相应的视频地址及视频的缩略图。

```java
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;
import org.jsoup.select.Elements;


/**
* 获取6间房视频
* @author sunlightcs
* 2011-3-31
* http://hi.juziku.com/sunlightcs/
*/
public class CN6Test {
	private static String URL = "http://6.cn/watch/14757577.html";
	public static void main(String[] args) throws Exception{
		
		Document doc = getURLContent();
		
		
		System.out.println("视频标题："+doc.title());
		
		/**
		 * 获取视频缩略图
		 */
		Element picEt = doc.getElementsByClass("summary").first();
		String pic = picEt.getElementsByTag("img").first().attr("src");
		System.out.println("视频缩略图："+pic);
		
		
		String time = getVideoTime(doc,"watchUserVideo");
		
		if(time==null){
			time = getVideoTime(doc,"watchRelVideo");
		}
		System.out.println("视频时长："+time);
		
		
		/**
		 * 获取视频地址
		 */
		Element flashEt = doc.getElementById("video-share-code");
		doc = Jsoup.parse(flashEt.attr("value"));  
		String flash = doc.select("embed").attr("src");
		
		System.out.println("视频地址："+flash);
		
	}

	/**
	 * 获取视频时长    
	 */
	private static String getVideoTime(Document doc, 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;
	}
	
	/**
	 * 获取6间房网页的内容
	 */
	private static Document getURLContent() throws MalformedURLException, IOException, UnsupportedEncodingException {
		Document doc = Jsoup.connect(URL)
		  .data("query", "Java")
		  .userAgent("Mozilla")
		  .cookie("auth", "token")
		  .timeout(3000)
		  .post();
		return doc;
	}

}
```


全文请访问：<http://www.juziku.com/wiki/772.htm>