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

昨日从某点评网爬取数据

网页编码为 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();
}
 

猜你喜欢

转载自seabay.iteye.com/blog/1585207