如何在忽略JavaScript和CSS的同时获取HTMLUnit中页面的纯原始HTML? [英] How to get the pure raw HTML of a page in HTMLUnit while ignoring JavaScript and CSS?

查看:74
本文介绍了如何在忽略JavaScript和CSS的同时获取HTMLUnit中页面的纯原始HTML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只希望页面的文本内容,并且希望抓取尽可能轻巧.我可以立即关闭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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆