我正在编写一个程序,该程序需要从输入文本文件中读取数据,并在查看日期时保存变量。我正在使用 Htmlunit,并且遇到了错误:
com.gargoylesoftware.htmlunit.ScriptException: Exception invoking open
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$HtmlUnitContextAction.run(JavaScriptEngine.java:684)
at net.sourceforge.htmlunit.corejs.javascript.Context.call(Context.java:602)
at net.sourceforge.htmlunit.corejs.javascript.ContextFactory.call(ContextFactory.java:507)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.callFunction(JavaScriptEngine.java:616)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.callFunction(JavaScriptEngine.java:591)
at com.gargoylesoftware.htmlunit.html.HtmlPage.executeJavaScriptFunctionIfPossible(HtmlPage.java:985)
at com.gargoylesoftware.htmlunit.javascript.host.EventListenersContainer.executeEventHandler(EventListenersContainer.java:210)
at com.gargoylesoftware.htmlunit.javascript.host.EventListenersContainer.executeBubblingListeners(EventListenersContainer.java:230)
at com.gargoylesoftware.htmlunit.javascript.host.Node.fireEvent(Node.java:804)
at com.gargoylesoftware.htmlunit.javascript.host.Node.fireEvent(Node.java:738)
at com.gargoylesoftware.htmlunit.html.HtmlElement$1.run(HtmlElement.java:869)
at net.sourceforge.htmlunit.corejs.javascript.Context.call(Context.java:602)
at net.sourceforge.htmlunit.corejs.javascript.ContextFactory.call(ContextFactory.java:507)
at com.gargoylesoftware.htmlunit.html.HtmlElement.fireEvent(HtmlElement.java:874)
at com.gargoylesoftware.htmlunit.html.HtmlElement.doClickFireClickEvent(HtmlElement.java:1311)
at com.gargoylesoftware.htmlunit.html.HtmlElement.click(HtmlElement.java:1253)
at com.gargoylesoftware.htmlunit.html.HtmlElement.click(HtmlElement.java:1205)
at com.gargoylesoftware.htmlunit.html.HtmlElement.click(HtmlElement.java:1160)
at Docketscraper.scrapeWebsite(Docketscraper.java:58)
at Docketscraper.starter(Docketscraper.java:40)
我这样做的代码是:
private static String startingMonth;
private static String startingDay;
private static String startingYear;
private static String endingMonth;
private static String endingDay;
private static String endingYear;
public static void starter() throws IOException{
Scanner sc = new Scanner("inputfile.txt").useDelimiter("\\s*|/");
while(sc.hasNext()) {
startingMonth = sc.next();
startingDay = sc.next();
startingYear = sc.next();
// skip "to"
sc.next();
endingMonth = sc.next();
endingDay = sc.next();
endingYear = sc.next();
scrapeWebsite();
}
}
scrapeWebsite 运行 htmlunit 方法的地方。scrapeWebsite 方法如下,它调用一个方法来解析数据:
public static void scrapeWebsite() throws IOException {
final WebClient webClient = new WebClient();
final HtmlPage page = webClient.getPage(url);
final HtmlForm form = page.getForms().get(0);
final HtmlElement button = form.getElementById("SheetContentPlaceHolder_C_searchresults_lbPrint");
final HtmlPage page2 = button.click();
try {
synchronized (page2) {
page2.wait(10000);
}
}
catch(InterruptedException e)
{
System.out.println("error");
}
originalHtml = page2.getWebResponse().getContentAsString();
obtainInformation();
originalHtml = "";
}
输入变量根据代码创建要搜索的 url:
private static String url = "http://cpdocket.cp.cuyahogacounty.us/SheriffSearch/results.aspx?q=searchType%3dCity%26searchString%3d%26foreclosureType%3d%26dateFrom%3d" + startingMonth + "%2f" + startingDay + "%2f" + startingYear + "+12%3a00%3a00+AM%26dateTo%3d" + endingMonth + "%2f" + endingDay + "%2f" + endingYear + "+11%3a59%3a59+PM";
这是特定于网站的 url。我认为这是扫描仪方法的一个问题,因为当我手动输入 6 个变量的数字并运行“scrapeWebsite”时,会出现正确的输出。我什至无法运行一组输入日期,其格式为:
1/1/2013 to 1/7/2013
我不确定“启动器”方法有什么问题