我想用htmlunit從網站上抓取數據。我將該地址作爲表單的屬性傳遞。即使我已導入.jar文件並正確設置了javadoc文件位置,但我仍然收到錯誤,它說「java.lang.NoClassDefFoundError:com/gargoylesoftware/htmlunit/WebClient」。我錯過了什麼嗎?在servlet上運行htmlunit
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文件放在哪裏? – 2012-07-30 21:20:41
你確定你有*所有必需的庫嗎?你如何指定類路徑? – 2012-07-30 21:22:46
我使用了構建類的路徑..並添加了外部的.jar文件(我把它放在項目文件夾中),但由於我希望路徑是絕對路徑,所以使用添加外部文件。我添加了從他們的網站下載的htmlunit .zip文件的全部內容。我還指定了javadoc位置。 – StackTraceYo 2012-07-30 21:30:23