2011-11-26 37 views
-2

我有困難從谷歌檢索50個結果。檢索從谷歌使用java的50個結果

我使用這個:

public static List Retrieve(String entry){ 
    List entryList = new ArrayList(); 

     try { 
      // string new entry is making the keywords into query that can be known by google 
      String newEntry = (java.net.URLEncoder.encode(entry, "UTF-8").replace("+", "%20")); 

      // inputing the keywords to google search engine 
      URL url = new URL("http://www.google.co.id/search?q=" + newEntry + "&hl=en&num=10&lr=&ft=i&cr=&safe=images&tbs="); 
      // makking connection to the internet 
      URLConnection urlConn = url.openConnection(); 
      urlConn.setUseCaches(false); 
      urlConn.setRequestProperty("User-Agent", "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13 (.NET CLR 3.5.30729)"); 

      // getting the input stream of page html into bufferedreader 
      BufferedReader buffReader = new BufferedReader(new InputStreamReader(urlConn.getInputStream())); 
      String line; 
      StringBuffer buffer = new StringBuffer(); 

     // getting the input stream of html into stringbuffer  
     while ((line = buffReader.readLine()) != null) { 
     buffer.append(line); 
     } 

     // finding the links 
     Pattern p = Pattern.compile(GOOGLE); 
     Matcher m = p.matcher(buffer.toString().toLowerCase()); 
     while (m.find()) { 
      String link = m.group(0); 
      // putting the links of google search into list 
      entryList.add(link); 
     } 
      } catch (Exception e) { 
      System.out.println(e.getMessage()); 
      } 

    return entryList; 
} 

但它只能說明只有十結果。

+0

你如何檢索你的數據? –

+0

嗯,我可以通過輸入一個搜索詞並瀏覽它們來獲得數百萬個結果...嚴重的是,您如何嘗試檢索這些結果?你能顯示任何代碼嗎? – Thomas

+0

我想檢索它們作爲列表,結果列表爲50個結果 – Carlo

回答

2

循環至5次,每次加10到「開始」在URL中GET變量

for(i=0;i<5;i++) 
{ 
    ... 
    URL url = new URL("http://www.google.co.id/search?q=" + newEntry + "&hl=en&num=10&lr=&ft=i&cr=&safe=images&tbs=&start="+(i*10)); 
    ... 
} 
+0

非常感謝它幫助很多 – Carlo

+0

將它標記爲答案或+1它呢? :) – Taz

0

那麼嘗試去到下一個頁面在谷歌搜索結果我覺得每一頁爲您提供了10個結果

+0

如何進入下一頁 – Carlo

+0

您更改頁面的網址參數 – Zimm3r

0

您只需要將URL中的num GET參數從10更改爲50.此參數表示要顯示多少個結果。