2009-02-11 100 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 !!!