如何在忽略JavaScript和CSS的同时获取HTMLUnit中页面的纯原始HTML? [英] How to get the pure raw HTML of a page in HTMLUnit while ignoring JavaScript and CSS?
问题描述
我只希望页面的文本内容,并且希望抓取尽可能轻巧.我可以立即关闭HTMLUnit所做的所有JavaScript和CSS以及其他外部内容的解析和其他加载吗?
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 other external content that HTMLUnit does out of the box?
推荐答案
我认为与您正在寻找的最接近的东西是:
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);
对于HtmlUnit 2.13及更高版本,请使用webclient.getOptions()
.
For HtmlUnit 2.13 and above, use webclient.getOptions()
.
此问题并回答可能也有用.它确实使我的工作变得更快,但是我不得不重新编译HtmlUnit ...
Also this question and answer might be useful too. It really made things faster for me, but I had to recompile HtmlUnit...
最后,为了获取页面的原始内容(而不是asXml()
的输出),请尝试以下操作:
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();
这篇关于如何在忽略JavaScript和CSS的同时获取HTMLUnit中页面的纯原始HTML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!