【问题标题】:Issue with parsing IPA symbols with JSOUP使用 JSOUP 解析 IPA 符号的问题
【发布时间】:2017-03-02 19:27:10
【问题描述】:

我正在尝试使用 jsoup 解析来自 www.wordreference.com 的英文单词转录。但是我在使用“æ”、“ǝ”等国际音标符号时遇到了麻烦。我总是得到问号,而不是这些符号。我尝试了不同的字符集,但它不起作用。这是代码:

public class TestClassParse {

    public static void main(String[] args) {

        Document doc;
        String transcription = "[ ]";
        try {

            String url = "http://www.wordreference.com/enru/word";
            String charset = "ISO-8859-1";

            doc = Jsoup.parse(new URL(url).openStream(), charset, url);
            Element transcriptionElement = doc.getElementById("pronWR");
            transcription = transcriptionElement.html();

        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println(transcription);
    }
}

这段代码的结果是 /w????d/ 而不是 /wɜːd/。我使用了不同的字符集,但得到了相同的结果。如何正确获取转录?

【问题讨论】:

  • 页面的字符集是什么?显然UTF-8。你为什么不使用它?使用不同的错误字符集进行测试的想法是什么?
  • 字符集是 UTF-8。我使用了 UTF-8,但仍然得到问号。我尝试了不同的字符集,因为 UTF-8 遇到了麻烦。
  • 为什么代码有String charset = "ISO-8859-1";?还要确保将结果显示在能够显示这些字符的位置。
  • 感谢您的 cmets!我刚刚尝试用 UTF-8 编码将结果写入文本文件,一切正常!看起来 IntelliJ IDEA 输出控制台不能很好地显示 UTF-8 字符集。
  • 可能是你没有正确配置IDEA。

标签: java jsoup html-parsing


【解决方案1】:

使用Jsoup.connect() 下载页面。这将自动使用 HTTP 响应中的字符集值并做正确的事情。

String url = "http://www.wordreference.com/enru/word";

String transcription = "[ ]";
try {
    Document document = Jsoup.connect(url).get();
    Element transcriptionElement = document.getElementById("pronWR");
    transcription = transcriptionElement.html();
} catch (IOException e) {
    e.printStackTrace();
}

System.out.println(transcription);

输出/wɜːd/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-06
    • 1970-01-01
    • 1970-01-01
    • 2019-09-30
    • 1970-01-01
    • 1970-01-01
    • 2015-06-06
    相关资源
    最近更新 更多