使用HTMLunit获取ajax / javascript内容

时间:2013-12-18 08:54:12

标签: java javascript ajax parsing htmlunit

我编写了一个代码,它会将页面的html内容作为响应提取给我,我正在使用HTML单元来执行此操作。但我正在为某些特定网址(如

)收到错误消息

[https://communities.netapp.com/welcome] [1]

对于第一页,我可以检索内容。但是,当我没有使用加载更多按钮的内容时。

这是我的代码:

    import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.net.MalformedURLException;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;


public class Sample {
    public static void main(String[] args) throws FailingHttpStatusCodeException, MalformedURLException, IOException, InterruptedException {
        String url = "https://communities.netapp.com/welcome";
        WebClient client = new WebClient(BrowserVersion.INTERNET_EXPLORER_9);
        client.getOptions().setJavaScriptEnabled(true);
        client.getOptions().setRedirectEnabled(true);
        client.getOptions().setThrowExceptionOnScriptError(true);
        client.getOptions().setCssEnabled(true);
        client.getOptions().setUseInsecureSSL(true);
        client.getOptions().setThrowExceptionOnFailingStatusCode(false);
        client.setAjaxController(new NicelyResynchronizingAjaxController());
        HtmlPage page = client.getPage(url);

        Writer output = null;
        String text = page.asText();
        File file = new File("D://write6.txt");
        output = new BufferedWriter(new FileWriter(file));
        output.write(text);
        output.close();
        System.out.println("Your file has been written");  

       // System.out.println("as Text ==" +page.asText());
      //  System.out.println("asXML == " +page.asXml());
      //  System.out.println("text content ==" +page.getTextContent());
      //  System.out.println(page.getWebResponse().getContentAsString());
    }
}

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

据我所知,你有一个按钮,你必须按下它。

请查看:http://htmlunit.sourceforge.net/gettingStarted.html

你有一个提交表格的例子。

这应该非常相似