[HtmlUnit]Fetch Dynamic Html/Content Created By Javascript/Ajax

import com.gargoylesoftware.htmlunit.*;
import com.gargoylesoftware.htmlunit.html.HtmlPage;

import java.net.*;
public class helloworld {

	public static void main(String[] args) throws Exception {  
		final WebClient webclient=new WebClient(BrowserVersion.FIREFOX_3_6);
		
		/*
		 * htmluni 2.11 version
		 * webclient.setJavaScriptEnabled(true);
		webclient.setCssEnabled(false);
		webclient.setAjaxController(new NicelyResynchronizingAjaxController());
		webclient.setTimeout(5000);
		webclient.setThrowExceptionOnScriptError(false);
		*/
		webclient.getOptions().setJavaScriptEnabled(true);
		webclient.getOptions().setCssEnabled(false);
		webclient.getOptions().setThrowExceptionOnScriptError(false);
		
		URL url=new URL("https://www.coursera.org/course/gametheory");
		HtmlPage page=webclient.getPage(url);
		Thread.sleep(1000*30);
		String ori = page.asXml();
		System.out.print(ori);
		
		
		//System.out.println(page.getHtmlElementById("cart-empty").asXml());
        
    }  
}
posted @ 2013-10-11 10:17  kkun  阅读(1322)  评论(0编辑  收藏  举报