前言
今天在测试爬虫项目时,发现了一个很严肃的问题,当爬取的网页编码格式为gb2312时,按照一般的办法转化为utf-8编码时总是乱码,PS:爬取的所有网页无论何种编码格式,都转化为utf-8格式进行存储。
一、问题出现
使用这篇文章里面的方法可以直接爬取页面信息并保存至本地http://stock.10jqka.com.cn/zhuanti/hlw_list/,发现使用之前(未知编码 -> utf-8编码)的转化方式总是乱码。于是乎查阅了不少资料,发现都不是太适用。最后自己摸索出了一个解决办法,也特此记录。
二、解决方案
1. 将gb2312格式转化为gbk格式
2. 将gbk格式转化为utf-8格式
这里的转化需要使用gbk作为一个中间格式,作为转化桥梁。
三、具体思路
1. 当打开http://stock.10jqka.com.cn/zhuanti/hlw_list/这个链接,我们查看源码会发现编码格式为gb2312,如下图所示
2. 由于本项目之前就已经使用了转化方案,但是此转化方案对网页为gb2312格式无效,本项目之前的转化方案的核心源代码为:
public void getContent(String url) { this.get = new HttpGet(url); HttpResponse response = client.execute(this.get); HttpEntity entity = response.getEntity(); byte[] bytes = EntityUtils.toByteArray(entity); String content = new String(bytes); // 默认为utf-8编码 String charset = "utf-8"; // 匹配<head></head>之间,出现在<meta>标签中的字符编码 Pattern pattern = Pattern.compile("<head>([\\s\\S]*?)<meta([\\s\\S]*?)charset\\s*=(\")?(.*?)\""); Matcher matcher = pattern.matcher(content.toLowerCase()); if (matcher.find()) { charset = matcher.group(4); } // 将目标字符编码转化为utf-8编码 String temp = new String(bytes, charset); byte[] contentData = temp.getBytes("utf-8"); return contentData; }