java中使⽤HtmlUnit爬⾍1.pom⽂件中添加依赖
<dependency>
<groupId>net.sourceforge.htmlunit</groupId>
<artifactId>htmlunit</artifactId>
<version>2.27</version>
</dependency>
2.写⼀个获取页⾯的util⽅法
public HtmlPage getHtmlPageResponse(WebClient webClient,String url) throws Exception {
webClient.setAjaxController(new NicelyResynchronizingAjaxController());//很重要,设置⽀持AJAX
webClient.setJavaScriptTimeout(timeout);//设置JS执⾏的超时时间
HtmlPage page;
try {
page = Page(url);
} catch (Exception e) {
webClient.close();
throw e;
}
webClient.waitForBackgroundJavaScript(waitForBackgroundJavaScript);//该⽅法阻塞线程
return page;
}
3.调⽤util⽅法
WebClient webClient = new WebClient();
java修改html文件
HtmlPage htmlPage = HtmlPageResponse(SourceUrl());
Document document = Jsoup.parse(htmlPage.asXml());//获取html⽂档
//处理document获取需要的内容

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。