Web Spider提取编码方法总结 (一)

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提取编码
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"));
 }

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值