前言

  前段时间写了个小说线上采集阅读(猛戳这里:https://www.cnblogs.com/huanzi-qch/p/9817831.html),当我们去采集起点网的小说目录时发现目录数据没有在html里面,数据是页面加载时,用ajax请求获取,且对应的div是隐藏的,需要点击“目录”,才看到目录,虽然经过研究最终我们还是找到了接口URL,并通过HttpClient构造post请求获取到了数据,但这种方式太麻烦,成本太大,那有没有其他的方式呢?

htmlUnit简单介绍

  通过查找资料发现一个神器:HtmlUnit 官网入口,猛戳这里:http://htmlunit.sourceforge.net

  以下介绍摘自官网:
  HtmlUnit is a "GUI-Less browser for Java programs". It models HTML documents and provides an API that allows you to invoke pages, fill out forms, click links, etc... just like you do in your "normal" browser.

  It has fairly good JavaScript support (which is constantly improving) and is able to work even with quite complex AJAX libraries, simulating Chrome, Firefox or Internet Explorer depending on the configuration used.

  It is typically used for testing purposes or to retrieve information from web sites.

  HtmlUnit is not a generic unit testing framework. It is specifically a way to simulate a browser for testing purposes and is intended to be used within another testing framework such as JUnit or TestNG. Refer to the document "Getting Started with HtmlUnit" for an introduction.

  HtmlUnit is used as the underlying "browser" by different Open Source tools like Canoo WebTest, JWebUnit, WebDriver, JSFUnit, WETATOR, Celerity, Spring MVC Test HtmlUnit, ...

  HtmlUnit was originally written by Mike Bowler of Gargoyle Software and is released under the Apache 2 license. Since then, it has received many contributions from other developers, and would not be where it is today without their assistance.

  HtmlUnit provides excellent JavaScript support, simulating the behavior of the configured browser (Firefox or Internet Explorer). It uses the Rhino JavaScript engine for the core language (plus workarounds for some Rhino bugs) and provides the implementation for the objects specific to execution in a browser.

  中文翻译:
  HtmlUnit是一个“Java程序的无界面浏览器”。它为HTML文档建模,并提供一个API,允许您调用页面、填写表单、单击链接等……就像你在“普通”浏览器中所做的一样。

  它有相当好的JavaScript支持(不断改进),甚至可以使用非常复杂的AJAX库,根据使用的配置模拟Chrome、Firefox或Internet Explorer。

  它通常用于测试或从web站点检索信息。

  HtmlUnit不是一个通用的单元测试框架。它是一种专门用于测试目的的模拟浏览器的方法,并打算在其他测试框架(如JUnit或TestNG)中使用。请参阅“开始使用HtmlUnit”文档以获得介绍。

  HtmlUnit被不同的开源工具用作底层的“浏览器”,比如Canoo WebTest, JWebUnit, WebDriver, JSFUnit, WETATOR, Celerity, Spring MVC Test HtmlUnit…

  HtmlUnit最初是由石像鬼软件的Mike Bowler编写的,在Apache 2许可证下发布。从那以后,它收到了其他开发者的许多贡献,如果没有他们的帮助,它就不会有今天的成就。

  HtmlUnit提供了出色的JavaScript支持,模拟了配置好的浏览器(Firefox或Internet Explorer)的行为。它使用Rhino JavaScript引擎作为核心语言(加上一些Rhino bug的解决方案),并为特定于在浏览器中执行的对象提供实现。

代码编写

  快速上手,猛戳这里:http://htmlunit.sourceforge.net/gettingStarted.html

  maven引包:

<dependency>
<groupId>net.sourceforge.htmlunit</groupId>
<artifactId>htmlunit</artifactId>
<version>2.32</version>
</dependency>

  那对应我们之前获取目录,我们可以这样做:

      try {
//创建一个WebClient,并模拟特定的浏览器
WebClient webClient = new WebClient(BrowserVersion.FIREFOX_52); //几个重要配置
webClient.getOptions().setJavaScriptEnabled(true);//激活js
webClient.setAjaxController(new NicelyResynchronizingAjaxController());//设置Ajax异步
webClient.getOptions().setThrowExceptionOnFailingStatusCode(true);//抛出失败的状态码
webClient.getOptions().setThrowExceptionOnScriptError(true);//抛出js异常
webClient.getOptions().setCssEnabled(false);//禁用css,无页面,无需渲染
webClient.getOptions().setTimeout(10000); //设置连接超时时间 //获取起点中文网书本详情、目录页面
HtmlPage page = webClient.getPage("https://book.qidian.com/info/1209977"); //设置等待js响应时间
webClient.waitForBackgroundJavaScript(5000); //模拟点击“目录”
page = page.getHtmlElementById("j_catalogPage").click(); //获取页面源代码
System.out.println(page.asXml());
} catch (IOException e) {
e.printStackTrace();
}

效果展示

  未执行js之前

  经过执行js请求渲染数据,再获取页面源代码,这样我们就能拿到带有目录数据的html了

结束语

  简单的几行代码就可以看出htmlUnit的强大,理论上,浏览器能做的它都能模拟;在这里先记录下来,等有空了再加到小说线上采集阅读(猛戳这里:https://www.cnblogs.com/huanzi-qch/p/9817831.html

最新文章

  1. 「译」JUnit 5 系列:条件测试
  2. C#中调用python方法
  3. CSS中font-size、font-family、line-height顺序以及简写属性
  4. Android -- 加载布局
  5. Hadoop集群添加新节点步骤
  6. C#编程简短总结
  7. 第三十一篇、iOS 9版本适配
  8. SQL 无法打开物理文件 XXX.mdf&quot;,操作系统错误 5:&quot;5(拒绝访问。)&quot;
  9. Eclipse在点击运行后不能自动保存的解决
  10. Search Bars(一个)
  11. H.264格式,iOS硬编解码 以及 iOS 11对HEVC硬编解码的支持
  12. python掉微信api
  13. C# 使用 SmtpClient.SendAsync 方法发送邮件失败,总是返回 Cancelled
  14. ansible基础-roles
  15. Python day 04
  16. Python—元类
  17. Java实现小学四则运算练习系统(UI)
  18. 一日游 + 进度psp
  19. java.lang.NoSuchMethodError: org.apache.commons.lang.StringUtils.endsWith(Ljava/lang/String;Ljava/lang/String;)Z
  20. HTML5-入门3。

热门文章

  1. java中的抽象类的作用
  2. 初识 Proxysql
  3. 解决 Files 的值&quot;&lt;&lt;&lt;&lt;&lt;&lt;&lt; HEAD&quot;无效。路径中具有非法字符
  4. php倒计时
  5. 关于select的使用感受~大坑~select不能添加点击事件触发~
  6. _ZNote_Qt_重启软件
  7. 写在HTTP协议之前
  8. nohup和&amp;后台运行,进程查看及终止
  9. JVM垃圾收集器与内存分配策略(一)
  10. 使用本地缓存快还是使用redis缓存好?