当前位置: 首页 > news >正文

htmlunit爬取js异步加载后的页面

直接上代码:

一、 index.html 
调用后台请求获取content中的内容。

<html>
<head>
    <script type="text/javascript" src="./jquery.min.js"></script> </head> <body> <h2>Hello World!</h2> <div id="content"></div> <script type="text/javascript"> $(document).ready(function(){ $.post("/evh/test/testList",{},function(data){ $("#content").text(JSON.stringify(data)); }); }); </script> </body> </html>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

二、TestController.java 
/test/testList接口从后台数据库获取数据。

package com.everhomes.proxy.controller; import javax.annotation.Resource; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.everhomes.proxy.mapper.TestMapper; @RestController @RequestMapping("/test") public class TestController { private static final Logger logger = LoggerFactory.getLogger(TestController.class); @Resource private TestMapper testMapper; @RequestMapping("testList") public Object testList(){ return testMapper.testList(); }; @ExceptionHandler(Exception.class) public Object exception(Exception e){ logger.error("error: ", e); return "error: " + e.toString(); } }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

三、Crawler.java

package com.everhomes.generate;

import java.io.IOException; import com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController; import com.gargoylesoftware.htmlunit.WebClient; import com.gargoylesoftware.htmlunit.html.HtmlPage; public class Crawler { public static void main(String[] args) throws IOException, InterruptedException { WebClient webClient = new WebClient(BrowserVersion.CHROME); webClient.getOptions().setJavaScriptEnabled(true); webClient.getOptions().setCssEnabled(false); webClient.getOptions().setRedirectEnabled(true); webClient.getOptions().setThrowExceptionOnScriptError(false); webClient.getOptions().setTimeout(50000); HtmlPage rootPage = webClient.getPage("http://localhost:8080/evh/index.html"); webClient.waitForBackgroundJavaScript(10000); FileUtils.createFile(DIRECTORY+"cc.html", rootPage.asXml()); webClient.close(); } } 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

四、pom.xml 
添加相关依赖。


    <dependency>
        <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version>2.6</version> </dependency> <dependency> <groupId>net.sourceforge.htmlunit</groupId> <artifactId>htmlunit-core-js</artifactId> <version>2.23</version> </dependency> <dependency> <groupId>net.sourceforge.htmlunit</groupId> <artifactId>htmlunit</artifactId> <version>2.25</version> </dependency> 

相关文章:

  • ruby gem 0.9.4的问题
  • Innodb与Myisam引擎的区别与应用场景
  • Linux中有两类函数库,分别是静态库和动态库
  • 记忆模糊的知识点5-22
  • 深入说明HDR技术
  • 免费产品体验码已发放完毕,实物礼品已备好?
  • wdcp服务器/虚拟主机管理系统1.1发布
  • VMware ESXi 和 VMware Server 有什么区别
  • mybatis-spring从1.1升级到1.2所带来的dao层级的编写问题
  • 802.11无线网络部署方案对比分析
  • RHEL中FQDN解析顺序。
  • @Autowired标签与 @Resource标签 的区别
  • 《唐之韵》解说词及古诗词
  • Java导出freemarker实现下载word文档格式功能
  • Insus Meta Utility
  • -------------------- 第二讲-------- 第一节------在此给出链表的基本操作
  • CSS魔法堂:Absolute Positioning就这个样
  • go语言学习初探(一)
  • HomeBrew常规使用教程
  • leetcode98. Validate Binary Search Tree
  • mysql中InnoDB引擎中页的概念
  • react 代码优化(一) ——事件处理
  • spring boot 整合mybatis 无法输出sql的问题
  • 开源SQL-on-Hadoop系统一览
  • 力扣(LeetCode)21
  • 前嗅ForeSpider教程:创建模板
  • 如何将自己的网站分享到QQ空间,微信,微博等等
  • 提升用户体验的利器——使用Vue-Occupy实现占位效果
  • 网页视频流m3u8/ts视频下载
  • 一起参Ember.js讨论、问答社区。
  • 一文看透浏览器架构
  • zabbix3.2监控linux磁盘IO
  • #在线报价接单​再坚持一下 明天是真的周六.出现货 实单来谈
  • (ISPRS,2023)深度语义-视觉对齐用于zero-shot遥感图像场景分类
  • (分享)自己整理的一些简单awk实用语句
  • (学习日记)2024.03.12:UCOSIII第十四节:时基列表
  • (转)scrum常见工具列表
  • (转)visual stdio 书签功能介绍
  • (转)甲方乙方——赵民谈找工作
  • (转)平衡树
  • **登录+JWT+异常处理+拦截器+ThreadLocal-开发思想与代码实现**
  • .htaccess 强制https 单独排除某个目录
  • .NET Core 控制台程序读 appsettings.json 、注依赖、配日志、设 IOptions
  • .net core 实现redis分片_基于 Redis 的分布式任务调度框架 earth-frost
  • .Net MVC4 上传大文件,并保存表单
  • .NET/C# 在代码中测量代码执行耗时的建议(比较系统性能计数器和系统时间)...
  • .Net下C#针对Excel开发控件汇总(ClosedXML,EPPlus,NPOI)
  • @hook扩展分析
  • @media screen 针对不同移动设备
  • @property python知乎_Python3基础之:property
  • [2021]Zookeeper getAcl命令未授权访问漏洞概述与解决
  • [52PJ] Java面向对象笔记(转自52 1510988116)
  • [AIGC] 开源流程引擎哪个好,如何选型?
  • [Android]通过PhoneLookup读取所有电话号码
  • [ARC066F]Contest with Drinks Hard