How to get the pure raw HTML of a page in HTMLUnit while ignoring JavaScript and CSS?

前端 未结 1 761
隐瞒了意图╮
隐瞒了意图╮ 2020-12-11 06:23

I just want the text content of page and I want the fetching to be as lightweight as possible. Can I turn off all the parsing and additional loading of JavaScript, CSS and o

相关标签:
1条回答
  • 2020-12-11 07:03

    I think the closest thing to what you're looking for is:

    WebClient webClient = new WebClient();
    webClient.setCssEnabled(false);
    webClient.setAppletEnabled(false);
    webClient.setJavaScriptEnabled(false);
    

    For HtmlUnit 2.13 and above, use webclient.getOptions().

    Also this question and answer might be useful too. It really made things faster for me, but I had to recompile HtmlUnit...

    Finally, in order to get the original content of the page (instead of the output of asXml()) try the following:

    WebClient webClient = new WebClient();
    HtmlPage page = webClient.getPage("http://www.yourpage.com");
    String originalHtml = page.getWebResponse().getContentAsString();
    
    0 讨论(0)
提交回复
热议问题