博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
htmlunit爬取js异步加载后的页面
阅读量:4179 次
发布时间:2019-05-26

本文共 2220 字,大约阅读时间需要 7 分钟。

直接上代码:

一、 index.html

调用后台请求获取content中的内容。

	

Hello World!

二、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();	}}

三、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();	}}

四、pom.xml

添加相关依赖。

commons-lang
commons-lang
2.6
net.sourceforge.htmlunit
htmlunit-core-js
2.23
net.sourceforge.htmlunit
htmlunit
2.25

欢迎关注我的公众号“彤哥读源码”,查看更多“源码&架构&算法”系列文章, 与彤哥一起畅游源码的海洋。

qrcode

转载地址:http://nseai.baihongyu.com/

你可能感兴趣的文章
字符数组和16进制互换
查看>>
PHP项目中出现致命错误: Class 'Redis' not found
查看>>
There is no tracking information for the current branch.
查看>>
fatal: refusing to merge unrelated histories
查看>>
Git命令还原未提交的变更
查看>>
Linux系统中环境变量的配置
查看>>
Linux系统中配置脚本程序开机启动
查看>>
让Linux系统上的nginx支持php程序
查看>>
源码编译安装LNMP环境之Nginx篇
查看>>
源码编译安装LNMP环境之PHP篇
查看>>
Linux中rpm工具使用教程
查看>>
Linux中yum工具使用教程
查看>>
C++字符串函数
查看>>
mknod详解
查看>>
linux中的run-level何解?
查看>>
Linux内核编译详解(转自linuxSir)
查看>>
实模式,保护模式与V86模式
查看>>
628. Maximum Product of Three Numbers(排序)
查看>>
Linux内核-------同步机制(二)
查看>>
面试题31-------连续子数组的最大和(数组)
查看>>