【发布时间】:2012-07-30 21:09:57
【问题描述】:
我想使用 htmlunit 从网站上抓取数据。我将地址作为表单的属性传递。我不断收到错误,它说“java.lang.NoClassDefFoundError:com/gargoylesoftware/htmlunit/WebClient”,即使我已导入 .jar 文件并正确设置了 javadoc 文件位置。我错过了什么吗?
package coreservlets;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlDivision;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
@WebServlet("/WebScrape")
@SuppressWarnings("serial")
public class WebScrape extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
// Create and initialize WebClient object
final WebClient webClient = new WebClient();
String Address = (String) request.getAttribute("address");
HtmlPage page = webClient.getPage(Address);
final HtmlDivision div = (HtmlDivision) page.getByXPath("//*[@id=\"LDPOffMarketPropertyInfo\"]//div//ul//li[4]//span[1]//text()");
out.println("<!DOCTYPE html>\n" +
"<html>\n" +
"<head>\n" +
"<meta name=" + "\"viewport\" " + "content=" + "\"initial-scale=1.0, user-scalable=no\" " + "/>\n" +
"<style type=" + "\"text/css\">\n" +
" html { height: 100% }\n" +
" body { height: 100%; margin: 0; padding: 0 }\n" +
" #default { height: 800px;\n"+
" width: 400px; }\n" +
" </style>\n" + div);
}
}
【问题讨论】:
-
解释“我已导入 .jar 文件”是什么意思。你把 jar 文件放在哪里了?
-
您确定您拥有所有所需的库吗?你是如何指定类路径的?
-
我使用了构建类路径 .. 并添加了外部 .jar 文件(我将其放在项目文件夹中),但使用 add external 因为我希望路径是绝对的。我添加了从他们的网站下载的 htmlunit .zip 文件的全部内容。我还指定了 javadoc 位置。