1
我有一個用Htmlunit編寫的程序,可以從網站上的多個頁面返回信息。問題是,前兩頁返回正常,然後返回其他所有頁面(返回頁面1,2,4,6等)。我相信我提到的所有變量都是正確的,所以我不確定問題是與服務器與程序進行通信還是其他問題。我的代碼是:Html單擊按鈕跳過頁面單擊
public static void scrapeWebsite() throws IOException {
final WebClient webClient = new WebClient();
final HtmlPage page = webClient.getPage(s);
originalHtml = page.getWebResponse().getContentAsString();
obtainInformation();
originalHtml = "";
//////code below returns page 2 as expected////
final HtmlForm form = page.getForms().get(0);
final HtmlSubmitInput button = form.getInputByValue(">");
final HtmlPage page2 = button.click();
try {
synchronized (page2) {
page2.wait(5000);
}
}
catch(InterruptedException e)
{
System.out.println("error");
}
originalHtml = originalHtml + page2.refresh().getWebResponse().getContentAsString();
obtainInformation();
originalHtml = "";
/////// code below returns page 4, instead of page 3/////
final HtmlForm form2 = page2.getForms().get(0);
final HtmlSubmitInput button2 = form2.getInputByValue(">");
final HtmlPage page3 = button2.click();
try {
synchronized (page3) {
page3.wait(5000);
}
}
catch(InterruptedException e)
{
System.out.println("error");
}
originalHtml = originalHtml + page3.refresh().getWebResponse().getContentAsString();
obtainInformation();
}
我能想到的唯一的問題是,當page.refresh()運行它本質上雙擊該按鈕,但沒有page.refresh()第1頁上的信息是剛剛返回了3次。另外,s是指網站的字符串。
刷新需要一些,而其他人不需要刷新。我相信網站的某些內容會導致每次迭代需要不同的代碼。 – Ctech45