爬虫 htmlUnit遇到Cannot locate declared field class org.apache.http.impl.client.HttpClientBuilder.dnsResolve错误

当在使用htmlUnit时遇到无法定位org.apache.http.impl.client.HttpClientBuilder.dnsResolver类时,此时所需要的依赖包为:

<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
</dependency>

运行maven之后的结果为:

此问题完美解决,接着运行程序出现一下错误:

java.lang.NoClassDefFoundError: org/w3c/dom/ElementTraversal

此问题主要时找不到xml解析包,解决方式:

<dependency>
<groupId>xml-apis</groupId>
<artifactId>xml-apis</artifactId>
<version>1.4.01</version>
</dependency>

此问题解决,接着在运行程序,出现一下错误
com.gargoylesoftware.htmlunit.ScriptException: Exception invoking getOffsetTop
这个错误不太好找,是htmlunit内部的错误,最终找到原因是由于解析抓取到的html页面时出的错
<dependency>
<groupId>xerces</groupId>
<artifactId>xercesImpl</artifactId>
<version>2.9.1</version>
</dependency>
xercesImpl包的版本过低导致该错误,升级为2.11.0版本即可
<dependency>
<groupId>xerces</groupId>
<artifactId>xercesImpl</artifactId>
<version>2.11.0</version>
</dependency>
遇到的问题基本都解决了
踩了两天坑终于可以正常爬取数据了!!!!

原文地址:https://www.cnblogs.com/leinuo2016/p/6252837.html