WebSpider提取编码方法总结
来源:互联网 发布:斗牛算法 编辑:程序博客网 时间:2024/05/17 13:41
概要:
1,通过分析Header提取编码
2,通过分析BOM(Byte Order Mark)提取编码
3,通过分析页面的meta提取编码
4,通过字节流分析检测编码
正文:
总结一下。目前有四种方法
1,通过分析Header提取编码。
这个也是比较简单。也是大家常用的,不过既然是总结。那还是帖一下代码吧。
2,通过分析BOM(Byte Order Mark)提取编码
说明:上面的用到的GetByteContent方法,在3中有;
3,通过分析页面的meta提取编码
这个也是大家常用。但是这里不需要抓两次。一次就可以分析了
说明:上面的用到的GetByteContent方法,在3中有;
3,通过分析页面的meta提取编码
这个也是大家常用。但是这里不需要抓两次。一次就可以分析了
4,通过字节流分析检测编码
就是一个byte一个byte的分析。网上的高手太多了,我这等菜鸟就不在这里献丑了。
已经有网友写出来了。高手Lion出品。2K多行代码。;)*—¥……#……#%……
我测试下。效果不错!可以从下面的URL获得需要的信息
http://www.cnblogs.com/lion.net/archive/2005/02/24/108395.html
5,一点问题
我在测试中。通过
和
结果竟然是不同的(前者正常,后者乱码)。
而Encoding.UTF8.Equals(Encoding.GetEncoding("utf-8"))是true
有那位朋友知道原因的。诚请指教。thanks!
1,通过分析Header提取编码
2,通过分析BOM(Byte Order Mark)提取编码
3,通过分析页面的meta提取编码
4,通过字节流分析检测编码
正文:
总结一下。目前有四种方法
1,通过分析Header提取编码。
这个也是比较简单。也是大家常用的,不过既然是总结。那还是帖一下代码吧。
复制 保存
WebRequest webRequest = WebRequest.Create(url);
HttpWebResponse webResponse = (HttpWebResponse) webRequest.GetResponse();
Regex reg_charset = new Regex(@"charset/b/s*=/s*(?<charset>[^""]*)");
WebHeaderCollection headers = webResponse.Headers;
string encodingName = string.Empty;
string contentType = headers["Content-Type"];
if (contentType.IndexOf("charset") > 0 && reg_charset.IsMatch(ContentType))
{
encodingName = reg_charset.Match(contentType).Groups["charset"].Value;
}
2,通过分析BOM(Byte Order Mark)提取编码
复制 保存
string encodingName = string.Empty;
WebRequest webRequest = WebRequest.Create(url);
HttpWebResponse webResponse = (HttpWebResponse) webRequest.GetResponse();
Stream stream = webResponse.GetResponseStream();
byte[] htmlByte = GetByteContent(stream);
string codingName = string.Empty;
Encoding encoding = Encoding.UTF8;
byte[] bomByte = encoding.GetPreamble();
if (htmlByte.Length > bomByte.Length && htmlByte[0] == bomByte[0]
&& htmlByte[1] == bomByte[1] && htmlByte[2] == bomByte[2])
{
codingName = "utf-8";
}
encoding = Encoding.Unicode;
bomByte = encoding.GetPreamble();
if (codingName == string.Empty && htmlByte.Length > bomByte.Length
&& htmlByte[0] == bomByte[0] && htmlByte[1] == bomByte[1])
{
codingName = "unicode";
}
encoding = Encoding.UTF32;
bomByte = encoding.GetPreamble();
if (codingName == string.Empty && htmlByte.Length > bomByte.Length
&& htmlByte[0] == bomByte[0] && htmlByte[1] == bomByte[1]
&& htmlByte[2] == bomByte[2] && htmlByte[3] == bomByte[3])
{
codingName = "utf-32";
}
encoding = Encoding.BigEndianUnicode;
bomByte = encoding.GetPreamble();
if (codingName == string.Empty && htmlByte.Length > bomByte.Length
&& htmlByte[0] == bomByte[0] && htmlByte[1] == bomByte[1])
{
codingName = "utf-16";
}
说明:上面的用到的GetByteContent方法,在3中有;
3,通过分析页面的meta提取编码
这个也是大家常用。但是这里不需要抓两次。一次就可以分析了
说明:上面的用到的GetByteContent方法,在3中有;
3,通过分析页面的meta提取编码
这个也是大家常用。但是这里不需要抓两次。一次就可以分析了
复制 保存
string encodingName = string.Empty;
WebRequest webRequest = WebRequest.Create(url);
HttpWebResponse webResponse = (HttpWebResponse) webRequest.GetResponse();
Stream stream = webResponse.GetResponseStream();
byte[] htmlByte = GetByteContent(stream);
stream.Close();
string temp = Encoding.GetEncoding("utf-8").GetString(htmlByte);
string reg_charset = "(<meta[^>]*charset=(?<charset>[^>'/"]*)[//s//S]*?>)|(xml[^>]+encoding=(/"|')*(?<charset>[^>'/"]*)[//s//S]*?>)";
Regex r = new Regex(reg_charset, RegexOptions.IgnoreCase);
Match m = r.Match(temp);
encodingName = (m.Captures.Count != 0) ? m.Result("${charset}") : "";
string html = Encoding.GetEncoding(encodingName).GetString(htmlByte);
//GetByteContent函数
private byte[] GetByteContent(Stream stream)
{
ArrayList arBuffer = new ArrayList();
byte[] buffer = new byte[1024];
int offset = 1024;
int count = stream.Read(buffer, 0, offset);
while (count > 0)
{
for (int i = 0; i < count; i++)
{
arBuffer.Add(buffer[i]);
}
count = stream.Read(buffer, 0, offset);
}
return (byte[]) arBuffer.ToArray(System.Type.GetType("System.Byte"));
}
4,通过字节流分析检测编码
就是一个byte一个byte的分析。网上的高手太多了,我这等菜鸟就不在这里献丑了。
已经有网友写出来了。高手Lion出品。2K多行代码。;)*—¥……#……#%……
我测试下。效果不错!可以从下面的URL获得需要的信息
http://www.cnblogs.com/lion.net/archive/2005/02/24/108395.html
5,一点问题
我在测试中。通过
复制 保存
StreamReader sr = new StreamReader(stream, Encoding.GetEncoding("utf-8"));
和
复制 保存
StreamReader sr = new StreamReader(stream, Encoding.UTF8);
结果竟然是不同的(前者正常,后者乱码)。
而Encoding.UTF8.Equals(Encoding.GetEncoding("utf-8"))是true
有那位朋友知道原因的。诚请指教。thanks!
- WebSpider提取编码方法总结
- Web Spider提取编码方法总结
- Web Spider提取编码方法总结 (一)
- Web Spider提取编码方法总结
- Web Spider提取编码方法总结
- webspider
- Web Spider提取编码方法总结(二)
- WebSpider的编码问题(乱码)浅析
- 视频前景提取方法总结
- opencv实现边缘提取方法总结
- 亚像素边缘提取方法总结
- JAVA WebSpider
- js字符串编码方法总结
- 字符编码转换方法总结
- 提取方法
- Java 提取文件编码
- 图像特征提取与特征选择基本方法总结
- C++私有数据成员提取到类外的方法总结
- IL语言学习(一)
- 简单的Txt文件名修改和扩展名修改
- ASP.NET 数据绑定常用代码
- 内存泄漏详解
- IL语言学习(一)
- WebSpider提取编码方法总结
- IT基础架构综合管理平台软件的面纱
- DB2创建数据库时的SQL1005N问题
- 英国人笑谈金融危机(轻松一刻)
- Oracle listener启动报错 分析及解决
- 刷新页面数据自动增加的问题
- 推荐几个好玩又有难度的编程网站
- IE7下表头显示不全要加的代码
- 泡沫破裂的经济学原理