2009-02-11 113 views
0

我試圖使用java.util.Scanner採取維基百科內容並將其用於基於詞的搜索。 事實是,這一切都很好,但是當閱讀一些文字時,它會給我錯誤。 看着代碼,並做了一些檢查,結果證明,有些詞似乎 不識別編碼,等等,而內容是不可讀的。 這是用來取頁面代碼:java.util.Scanner和Wikipedia

// -Start-

try { 
     connection = new URL("http://it.wikipedia.org 
wiki/"+word).openConnection(); 
        Scanner scanner = new Scanner(connection.getInputStream()); 
     scanner.useDelimiter("\\Z"); 
     content = scanner.next(); 
//   if(word.equals("pubblico")) 
//    System.out.println(content); 
     System.out.println("Doing: "+ word); 
//End 

的問題的話爲「共和」的意大利語維基百科出現。上字公衆大樓中的println的 結果是這樣的(板缺): ï¿ï¿½] KSR>�〜戊 �1A���E�ER3tHZ�4v��&PZjtcï ¿½¿½ï¿½D�7_|����=8��Ø}

你知道爲什麼嗎?然而看着頁面源代碼和標題是相同的,使用相同的編碼...

原來,內容是gzipped,所以我可以告訴維基百科不要給我teir頁拉鍊或它的唯一途徑?謝謝

+0

我更新了我的答案以解決您的gzip問題。 – erickson 2009-02-11 22:37:10

回答

1

嘗試使用的Reader而不是InputStream - 我認爲它的工作原理是這樣的:

connection = new URL("http://it.wikipedia.org/wiki/"+word).openConnection(); 
String ctype = connection.getContentType(); 
int csi = ctype.indexOf("charset="); 
Scanner scanner; 
if (csi > 0) 
    scanner = new Scanner(new InputStreamReader(connection.getInputStream(), ctype.substring(csi + 8))); 
else 
    scanner = new Scanner(new InputStreamReader(connection.getInputStream())); 
scanner.useDelimiter("\\Z"); 
content = scanner.next(); 
if(word.equals("pubblico")) 
    System.out.println(content); 
System.out.println("Doing: "+ word); 

你也可以只通過字符集到掃描儀的構造函數直接作爲中指出另一個答案。

+0

請勿使用內容編碼。它指定使用的壓縮,並且與字符編碼無關。 – erickson 2009-02-11 22:07:33

2

嘗試使用掃描儀用指定的字符集:

public Scanner(InputStream source, String charsetName) 

對於默認的構造函數:

從流

字節轉換成使用底層平臺的默認字符集字符。

Scanner on java.sun.com

0
connection = new URL("http://it.wikipedia.org/wiki/"+word).openConnection(); 
      connection.addRequestProperty("Accept-Encoding",""); 
      System.out.println(connection.getContentEncoding()); 
      Scanner scanner = new Scanner(new InputStreamReader(connection.getInputStream())); 
      scanner.useDelimiter("\\Z"); 
      content = new String(scanner.next()); 

編碼不會改變。爲什麼?

0
connection = new URL("http://it.wikipedia.org/wiki/"+word).openConnection(); 
//connection.addRequestProperty("Accept-Encoding",""); 
//System.out.println(connection.getContentEncoding()); 

InputStream resultingInputStream = null;  // Stream su cui fluisce la pagina scaricata 
String encoding = connection.getContentEncoding(); // Codifica di invio (identity, gzip, inflate) 
// Scelta dell'opportuno decompressore per leggere la sorgente 
if (connection.getContentEncoding() != null && encoding.equals("gzip")) { 
    resultingInputStream = new GZIPInputStream(connection.getInputStream()); 
} 
else if (encoding != null && encoding.equals("deflate")) { 
    resultingInputStream = new InflaterInputStream(connection.getInputStream(), new Inflater(true)); 
} 
else { 
    resultingInputStream = connection.getInputStream(); 
} 

// Scanner per estrarre dallo stream la pagina per inserirla in una stringa 
Scanner scanner = new Scanner(resultingInputStream); 
scanner.useDelimiter("\\Z"); 
content = new String(scanner.next()); 

So works !!!