2012-02-17 20 views
0

Google検索結果の最初の2ページを取得するには以下のコードを使用しますが、最初のページしか取得できません(検索ページ2のときは同じです1ページ)htmlunitの使い方googleの次のページを取得

import com.gargoylesoftware.htmlunit.WebClient; 
import com.gargoylesoftware.htmlunit.html.HtmlElement; 
import com.gargoylesoftware.htmlunit.html.HtmlPage; 
import com.gargoylesoftware.htmlunit.html.HtmlTextInput; 


/** 
* A simple Google search test using HtmlUnit. 
* 
* @author Rahul Poonekar 
* @since Apr 18, 2010 
*/ 
public class Author_search { 
    static final WebClient browser; 

    static { 
     browser = new WebClient(); 
     browser.setJavaScriptEnabled(false); 
    } 

    public static void main(String[] arguments) { 
      searchTest(); 
    } 

    private static void searchTest() { 
     HtmlPage currentPage = null; 

     try { 
      currentPage = (HtmlPage) browser.getPage("http://www.google.com"); 
     } catch (Exception e) { 
      System.out.println("Could not open browser window"); 
      e.printStackTrace(); 
     } 
     System.out.println("Simulated browser opened."); 

     try { 
      ((HtmlTextInput) currentPage.getElementByName("q")).setValueAttribute("xxoo"); 
      currentPage = currentPage.getElementByName("btnG").click(); 
      System.out.println("contents: " + currentPage.asText()); 
      HtmlElement next = (HtmlElement)currentPage.getByXPath("//span[contains(text(), 'Next')]").get(0); 
      currentPage = next.click(); 
      System.out.println("contents: " + currentPage.asText()); 
     } catch (Exception e) { 
      System.out.println("Could not search"); 
      e.printStackTrace(); 
     } 
    } 
} 

誰でもこの問題を解決する方法を教えていただけますか?道による

  1. どうhtmlunitを使用して、Googleでの言語設定を変更するには?任意 便利な方法?
  2. firefoxでhtmlunitを "firebug"のように扱うか、 "file-> save"のテキストのように扱いますか。私の の意見では、それはエクスプローラのように扱うと信じています。 ?

答えて

2

私が交換:

HtmlAnchor nextAnchor =currentPage.getAnchorByText("Next"); 
currentPage = nextAnchor.click(); 
+0

インポートする必要があります:と

HtmlElement next = (HtmlElement)currentPage.getByXPath("//span[contains(text(),'Next')]").get(0); currentPage = next.click(); 

をcom.gargoylesoftware.htmlunit.html.HtmlAnchor –

関連する問題