2013-06-21 18 views
0

我們想以編程方式取當前的谷歌頁面。我們使用不同的編程語言的許多技術,但我們無法獲得正確的(當前)谷歌頁面。以編程方式拍攝的頁面與正常的Google頁面不同?

的Java代碼示例

public class GoogleParser { 

public static void main(String[] args){ 
     GoogleParser googleParser = new GoogleParser(); 
     googleParser.execute(); 
} 
public void execute(){ 
String[] params = {"ankara nüfusu"};  
    final URL url = encodeGoogleQuery(params); 

     System.out.println("Downloading [" + url + "]...\n\n\n\n\n"); 
     try { 
final String html = downloadString(url); 
System.out.println(html); 
} catch (IOException e) { 
// TODO Auto-generated catch block 
e.printStackTrace(); 
} 
} 
private static String downloadString(final InputStream stream) throws IOException { 
final ByteArrayOutputStream out = new ByteArrayOutputStream(); 
int ch; 
while (-1 != (ch = stream.read())) 
    out.write(ch); 
return out.toString(); 
} 
    private static String downloadString(final URL url) throws IOException { 
     final String agent = "Mozilla/21.0 (Windows; U; Windows 7; en-US)"; 
     final URLConnection connection = url.openConnection(); 
     connection.setRequestProperty("User-Agent", agent); 
     final InputStream stream = connection.getInputStream(); 
     return downloadString(stream); 
    } 

private static URL encodeGoogleQuery(final String[] args) { 
     try { 
      final StringBuilder localAddress = new StringBuilder(); 
      localAddress.append("https://stackoverflow.com/search?q="); 

      for (int i = 0; i < args.length; i++) { 
       final String encoding = URLEncoder.encode(args[i], "UTF-8"); 
       localAddress.append(encoding); 
       if (i + 1 < args.length) 
        localAddress.append("+"); 
      } 

      return new URL("http", "www.google.com", localAddress.toString()); 

     } catch (final IOException e) { 
      // Errors should not occur under normal circumstances. 
      throw new RuntimeException(
        "An error occurred while encoding the query arguments."); 
     } 
    } 
} 

Java Code get this html page Google current Page

First image Java Code Result Page 
Second image Google Current Page 

HTML頁面從Java谷歌獲得比目前的谷歌網頁不同。

  1. 不同的結果
  2. 不包含谷歌現在的結果(4551 milyon(2011)的一部分)
  3. 不包含谷歌圖的結果(右側安卡拉信息),比當前
  4. 導航性能
  5. 舊頁(網站,圖片,視頻)左側,通常搜索欄下方

你有什麼想法如何獲得當前(最後一頁)的谷歌與p用Java編程語言。但是其他語言的解決方案對於解決問題很重要。

感謝您迴應

+1

請注意,您已在第二個屏幕截圖中登錄Google。搜索結果可能根據您的搜索歷史進行個性化。嘗試註銷或更好地使用瀏覽器的隱身/隱私瀏覽模式。 – JJJ

+0

另外,您正在使用未知的用戶代理。嘗試指定一個[有效](http://www.useragentstring.com/pages/Firefox/)。根據我的經驗,谷歌對用戶代理非常敏感。 – MCL

回答

0

谷歌是聰明在檢測誰在發送請求的方式:

  1. 確保您發送相同的餅乾爲你的瀏覽器
  2. 確保你發送相同或有效的瀏覽器代理字符串
相關問題