java - 抓取俄罗斯网站时出现垃圾字符

标签 java linux encoding web-crawler cyrillic

我正在尝试在 Linux 中抓取一个俄语网站,但输出似乎是垃圾字符。 该网站采用 UTF-8 编码,在阅读时我将编码设置为 UTF-8。然而这似乎并不能解决问题。 我应该做什么来阅读它?

public class Crawl {   
    @SuppressWarnings("unused")
    public static void main(String[] args) {

    URL my_url = new URL("http://www.fmsmoscow.ru/docs/migration_registration/registration.html");
    BufferedReader br = new BufferedReader(new InputStreamReader(my_url.openStream(),"UTF-8"));
    while (null != (strTemp = br.readLine())){
    System.out.println(strTemp);
        }
    }   
}

上面是相同的代码。 我将代码导出为 jar 并将其添加到 Linux 服务器中。 然后我执行它以获取 Linux 控制台中的输出。

最佳答案

一般来说,您还可以(按原样)存储内容二进制文件,然后查看哪里出了问题。比如说在 JEdit 或 NotePad++ 等可以切换编码的程序员编辑器中。它可能会被隐藏在 native Windows-1251 的 HTML 注释中。 也许剥离 HTML 注释会有所帮助。

对于容错解码、错误报告,需要 CharsetDecoder .

CharsetDecoder decoder = StandardCharsets.UTF_8.newDecoder(); // ISO_8859_1
decoder.onMalformedInput(CodingErrorAction.IGNORE);
InputStreamReader reader = new InputStreamReader(my_url.openStream(), decoder);

: 请参阅有关 REPLACE、REPORT 的 javadoc。如果你想要更多,方法就不同了。您需要读取输入,将字节放入 ByteBuffer 中,然后

CoderResult result = decoder(byteBuffer, charBuffer, true /*EOF*/);
<小时/>

要从 UTF-8 中挑选西里尔字母序列,可以检查 UTF-8 序列的有效性:

  • 0xxxxxxx
  • 110xxxxx 10xxxxxx
  • 1110xxxx 10xxxxxx 10xxxxxx
  • ...

因此(未经测试):

void patchMixOfUtf8AndCp1251(byte[] bytes, StringBuilder sb) {
    boolean priorWrongUtf8 = false;
    for (int i = 0; i < bytes.length; ++i) {
        byte b = bytes[i];
        if (b >= 0) {
            sb.appendCodePoint((int)b);
            priorWrongUtf8 = false;
        } else {
            int n = highBits(b); // Also # bytes in sequence
            boolean isUTF8 = !priorWrongUtf8
                    && 1 < n && n <= 6
                    && i + n <= bytes.length;
            if (isUTF8) {
                for (int j = 1; j < i + n; ++j) {
                     if (highBits(bytes[j]) != 1) {
                         isUTF8 = false;
                         break;
                     }
                }
            }
            if (isUTF8) {
                sb.append(new String(bytes, i, i + n, StandardCharsets.UTF_8));
                i += n - 1;
            } else {
                // UTF-8 Continuation char must be Cp1252.
                sb.append(new String(bytes, i, i + 1, "Windows-1251"));
            }
            priorWrongUtf8 = !isUTF8;
        }
    }
}

private int highBits(byte b) {
    int n = 0;
    while (n < 8 && ((1 << (7 - n)) & b) != 0) {
        ++n;
    }
    return n;
}

由于西里尔字母不太可能立即连接到 UTF-8,因此使用状态 priorWrongUtf8

关于java - 抓取俄罗斯网站时出现垃圾字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29124804/

相关文章:

linux - 在 ubuntu 中使用互联网设置 NTP 服务器

php - Linux:文件既存在又不存在

python - 如何使用 python 对字符串进行 url 安全编码?并且 urllib.quote 是错误的

java - 使用 JSR-303 验证表单时使用 I18n 消息

java - 如何在java中获取第三个斜杠之后的字符串

java - 下载xml,删除bom并编码utf8

c - 在linux中使用execv使用 './'命令

python - 如何使用正则表达式 Python 在文件中查找非 ASCII 字符

python - 如何处理来自 urllib.request.urlopen() 的响应编码,以避免 TypeError : can't use a string pattern on a bytes-like object

Java 套接字 Json