【问题标题】:Using HTMLUnit to get javascript elements使用 HTMLUnit 获取 javascript 元素
【发布时间】:2013-03-08 16:17:06
【问题描述】:

我正在尝试使用 HTMLUnit 来获取网页 (https://www.coursera.org/courses) 上的 javascript 元素,它只加载 html 数据。如何让它显示 javascript 容器中显示的信息?

谢谢!

我当前的代码:

     public String DownloadPage(String str){
    final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_3_6);
    webClient.getOptions().setTimeout(20000);
    webClient.getOptions().setJavaScriptEnabled(true);
    webClient.getOptions().setThrowExceptionOnScriptError(false);

    try{
        HtmlPage page = webClient.getPage(str);
        XmlPage page2 = webClient.getPage(str);
        int n = webClient.waitForBackgroundJavaScript(100000); 

        System.out.println("Executing " + n + " JavaSript jobs!"); 
        System.out.println("OUTPUT: " + page2); 

        System.out.println("OUTPUT: " + page.asXml()); 
        webClient.closeAllWindows(); 
    }

    catch(IOException e){
        JOptionPane.showMessageDialog(null, "error");
    }


    webClient.closeAllWindows();
    return "";
}

【问题讨论】:

    标签: html xml-parsing html-parsing web-scraping htmlunit


    【解决方案1】:

    使用

    String theContent1 = webClient.getPage(theURL).getWebResponse().getContentAsString();
    

    而不是

    String theContent2 = webClient.getPage(theURL);
    

    theContent1 应该包含实际的页面源代码,包括 JavaScript(如果有的话)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多