我想使用 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);
}
}