0

java.util.Scannerを使用してウィキペディアのコンテンツを取得し、単語ベースの検索に使用しようとしています。事実、それはすべて問題ありませんが、いくつかの単語を読むとエラーが発生します。コードを見て確認すると、一部の単語ではエンコーディングなどが認識されないようで、コンテンツが読みにくくなっていることがわかりました。これは、ページを取得するために使用されるコードです。

// -始める-

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

この問題は、イタリア語版ウィキペディアの「pubblico」という単語で発生します。pubblicoという単語のprintlnの結果は次のようになります(カット):ï¿ï¿½] Ksr>�〜E�1A���E�ER3tHZ�4v��&PZjtc�¿½ï¿ ½D�7_|����=8��Ø}

理由はわかりますか?しかし、ページのソースとヘッダーは同じで、同じエンコーディングです...

コンテンツがgzipで圧縮されていることが判明したので、ウィキペディアにteirページを圧縮して送信しないように指示できますか、それが唯一の方法ですか?ありがとうございました

4

5 に答える 5

2

指定した文字セットでスキャナを使用してみてください。

public Scanner(InputStream source, String charsetName)

デフォルトのコンストラクターの場合:

ストリームからのバイトは、基盤となるプラットフォームのデフォルトの文字セットを使用して文字に変換されます。

java.sun.com のスキャナ

于 2009-02-11T21:58:08.960 に答える
1

Readerの代わりにa を使用してみてください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);

別の回答で示されているように、文字セットを Scanner コンストラクターに直接渡すこともできます。

于 2009-02-11T22:02:35.657 に答える
1

を使用して、応答のcontent-type ヘッダーURLConnectionを判別できるようにする必要があります。これにより、 を作成するときに使用する文字エンコーディングがわかります。Scanner

具体的には、コンテンツ タイプ ヘッダーの「charset」パラメータを見てください。


gzip 圧縮を禁止するには、accept-encoding ヘッダーを「identity」に設定します。詳細については、HTTP 仕様を参照してください。

于 2009-02-11T22:03:41.397 に答える
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());

エンコーディングは変更されません。なぜ?

于 2009-02-12T16:14:44.003 に答える
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());

とてもうまくいきます!!!

于 2009-02-12T22:37:04.537 に答える