1. 程式人生 > >利用httpClient和htmlParse獲取網頁iframe資料

利用httpClient和htmlParse獲取網頁iframe資料

public static void main(String[] args) {	  
	  HttpClient client = new HttpClient();
	  HttpMethod method = new GetMethod("http://www.ln.gov.cn/video/video_57835_1/zydst/2011_99870/d12q/201401/t20140101_1249267.html");
	  try {
		  
		  client.executeMethod(method);
		  
		  Parser parser = new Parser(method.getResponseBodyAsString());  
		  NodeVisitor visitor = new NodeVisitorExtends();
		  parser.visitAllNodesWith(visitor);
		  
		  
		  method.releaseConnection();   
	} catch (IOException e) {
		e.printStackTrace();
	} catch (ParserException e) {
		e.printStackTrace();
	}
  }


/**
   * 定義內部類,獲取抓取的網頁資料中iframe的src包含http://的值。
   * 2014-08-14 16:52:10
   * @author pengyh
   *
   */
  private static class NodeVisitorExtends extends NodeVisitor {  
      public void visitTag(Tag tag) {  
    	  //只獲取html中iframe節點的屬性
          if(tag.getTagName().equalsIgnoreCase("iframe")){
        	  //獲取該iframe中的src屬性
        	  String srcUrl = tag.getAttribute("src");
        	  //只獲取該src中包含有http://的值
        	  if(srcUrl.contains("http://")){
        		  logger.info("獲取到iframe中包含http:的地址為[{}]", srcUrl);
        		  System.out.println(srcUrl);
        	  }
          }
      }  
  } 

定義的內部類NodeVisitorExtends,可以對抓取到的網頁資料進行處理。測試中的為獲取iframe中src的屬性。

======================以上方法只能獲取到flash播放器地址,如果正常的網頁播放視訊,wap頁面使用<iframe src="">便可以正常播放,但是如果wap為嵌入客戶端框架,如果該安卓客戶端框架整合的播放器為手機自身播放器,有時候需要使用HTML5中<video>標籤,此時,就需要獲取該flash播放器中真正的視訊mp4地址。

此時需要做修改為:

String MP4Url= parseContent("http://video.ln.gov.cn:8080/mas/front/video/main.do?method=exPlay&id=659721&autoPlay=true&logoAlpha=0&logoCss=lb",  "utf-8");

http://video.ln.gov.cn:8080/mas/front/video/main.do?method=exPlay&id=659721&autoPlay=true&logoAlpha=0&logoCss=lb為上面抓到的flash播放器地址,將其轉成ipad協議或在重新解析。(但是貌似還是會有些視訊地址抓不到地址,可能跟視訊源有關係。)

MP4Url就為真正視訊地址。

/**
	 * 上面的方法只是獲取iframe,src flash播放器的地址, 此方法獲取flash播放器的視訊地址 2014-09-22 16:45:29
	 * 
	 * @author pengyh
	 * @param url
	 * @param charset
	 * @return
	 */
	public static String parseContent(String url, String charset) {
		logger.info("獲取flash播放器中的視訊地址,url:{}", url);
		InputStream is = null;
		HttpClient client = null;
		Document doc = null;
		try {
			client = new HttpClient();
			client.getParams().setParameter(HttpMethodParams.USER_AGENT,
					"Mozilla/5.0 (iPad; CPU OS 7_0 like Mac OS X)");
			GetMethod getMethod = new GetMethod(url);
//			client.getHostConfiguration().setProxy("192.168.13.19", 7777);
			client.executeMethod(getMethod);
			is = getMethod.getResponseBodyAsStream();
			doc = Jsoup.parse(is, charset, "");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (is != null)
				try {
					is.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			client = null;
		}
		logger.info("獲取返回結果中source節點的標籤");
		Elements tag = doc.getElementsByTag("source");
		if (tag != null) {
			String srcVal = tag.attr("src");
			logger.info("獲取source標籤中src的值[{}]。", srcVal);
			return srcVal;
		}
		return null;
	}


需要引入jar包:httpClient.har、htmlparse.jar、htmllexer.jar

jar包下載地址:http://download.csdn.net/detail/p793049488/7756001