[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());
        
    }  
}

你可能感兴趣的:(JavaScript)