我正在使用HtmlUnit以编程方式填写并提交Java中的Web表单。这是我的代码:
WebClient client = new WebClient();
client.setThrowExceptionOnScriptError(false); // avoid JavaScript errors
client.setTimeout(120000); // 2 minutes
HtmlPage page;
// load the website
page = client.getPage("http://www.some-website.com");
// represent the page elements in Java objects
// input fields and checkboxes first, then...
HtmlSubmitInput submit = form.getInputByName("submitbutton");
// set "value" attributes of input fields and checkboxes...
// submit the page
System.out.println("Submitting... ");
page = submit.click();
System.out.println("Done!");
// return the resulting HTML for scraping
return page.asXml();
现在,在submit.click()
部分,我不断收到以下异常:
java.net.SocketTimeoutException: Timeout while fetching: http://www.some-website.com
我理解这是因为我正试图从2002年一直到今天检索数据。从我的浏览器加载它,整个过程通常需要大约六分钟左右,并返回大约24,200行数据。
我计算了从Submitting...
打印出来直到SocketTimeoutException
被抛出的时间,并且在所有情况下,即使我将client
超时设置为2分钟。现在,我知道这是最初加载页面的超时(client.getPage(...)
调用),所以有没有办法让我设置按钮点击的超时并使其等待超过一分钟,可能是十分钟?
答案 0 :(得分:1)
在撰写本文时,没有已知的解决方案。我最终做的是在页面上发出多个自动请求以接收部分数据。基本上,我首先查询2002年,然后是2003年,2004年,依此类推。