ITeye博客

数据爬取 -- 编码(UTF-8 to GBK)

seabay · 2012-07-11 17:18 · Network · 互联网 · Markdown

昨日从某点评网爬取数据

网页编码为 UTF-8, 本地的系统默认编码为 GBK

直接使用 BufferReader 读取 HttpURLConnection 打开的 stream,会导致乱码

使用 DataInputStream 的 readUTF 也引起乱码

最后使用如下代码,得到正确的文本,如下:

BufferedReader dis;

String content = new String();
String line;
try {

    dis = new BufferedReader(new InputStreamReader(is, "UTF-8"));

    while ((line = dis.readLine()) != null) {
        content += line;
    }
    dis.close();
    String nct = new String(content.getBytes("GBK"));
    } catch (Exception e) {
        e.printStackTrace();
}

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。