手机站
网通分站
电信主站
密 码:
用户名:
当前位置 : 主页>网络编程>Asp.Net编程>列表

解决网爬工具爬取页面信息出现乱码的问题

来源:互联网 作者:west263.com 时间:2008-02-22
西部数码-全国虚拟主机10强!40余项虚拟主机管理功能,全国领先!双线多线虚拟主机南北访问畅通无阻!免费赠送企业邮局,.CN域名,自助建站480元起,免费试用7天,满意再付款! P4主机租用799元/月.月付免压金!

问题:
网爬工具中自动搜集页面信息时,有的页面出现了出现乱码现象
原因:
读取页面信息是使用了错误的编码类型。C#.NET从现在的类中获取得来的编码信息有时是错误的,本人认为对不是ASP.NET的应用程序,它读过来的编码信息都是错误的。
解决:
思路:必须先在运行时获取得该页面的编码,再去读取页面的内容,这样得来的页面内容才不会出现乱码现象。
方法:
1:使用ASCII编码去读取页面内容。
2:使用正则表达式从读取的页面内容中筛选出页面的编码信息。上个步骤获取的页面信息可能会有乱码。但HTML标志是正确的,所有可以从HTML标志中得到编码的信息。
3.用正确的编码类型去读取页面信息。
如果哪位有更好的方法,请多赐教啊!


下面附上代码:

代码演示
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Web;
using System.IO;
using System.Text.RegularExpressions;
namespace charset
{
class Program
{

static void Main(string[] args)
{
string url = "http://www.gdqy.edu.cn";
GetCharset1(url);
GetChartset2(url);

Console.Read();
}
// 通过HttpWebResponse直接获取页面编码
static void GetCharset1(string url)
{
try
{
WebRequest webRequest = WebRequest.Create(url);
HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();

string charset = webResponse.CharacterSet;
string contentEncoding = webResponse.ContentEncoding;
string contentType = webResponse.ContentType;

Console.WriteLine("context type:{0}", contentType);

Console.WriteLine("charset:{0}", charset);

Console.WriteLine("content encoding:{0}", contentEncoding);
//测试或取页面是否出现乱码
//Console.WriteLine(getHTML(url,charset));

}
catch (UriFormatException ex)
{

Console.WriteLine(ex.Message);
}
catch(WebException ex)
{

Console.WriteLine(ex.Message);
}


}
//使用正则表达式获取页面编码
static void GetChartset2(string url)
{

try
{
string html = getHTML(url,Encoding.ASCII.EncodingName);
Regex reg_charset = new Regex(@"charset\b\s*=\s*(?<charset>[^""]*)");
string enconding = null;
if (reg_charset.IsMatch(html))
{
enconding = reg_charset.Match(html).Groups["charset"].Value;
Console.WriteLine("charset:{0}",enconding);
}
else
{
enconding = Encoding.Default.EncodingName;
}
//测试或取页面是否出现乱码
//Console.WriteLine(getHTML(url,enconding));
}
catch (UriFormatException ex)
{

Console.WriteLine(ex.Message);
}
catch(WebException ex)
{

Console.WriteLine(ex.Message);
}

}
//读取页面内容方法
static string getHTML(string url,string encodingName)
{

try
{
WebRequest webRequest = WebRequest.Create(url);
WebResponse webResponse = webRequest.GetResponse();
Stream stream = webResponse.GetResponseStream();
StreamReader sr = new StreamReader(stream, Encoding.GetEncoding(encodingName));
string html = sr.ReadToEnd();
return html;
}
catch (UriFormatException ex)
{

Console.WriteLine(ex.Message);
return null;
}
catch (WebException ex)
{

Console.WriteLine(ex.Message);
return null;
}
}

}
}

http://www.gdqy.edu.cn页面的使用的编码格式是:gb2312
第一个方法显示的内容是:
context type:text/html
charset:ISO-8859-1
content encoding:
第二个方法显示的内容是:
charset:gb2312

所以第一个方法获取的信息是错误的,第二个方法是对的。
为什么第一个方法获取的的编码格式是:ISO-8859-1呢?
我用Reflector反射工具获取了CharacterSet属性的源代码,从中不难看出其原因。如果能获取出ContentType属性的源代码就不以看出其出错的原因了,但是搞了许久都没找出,如果那位那补上,那就太感谢了。
下面我附上Reflector反射工具获取了CharacterSet属性的源代码,有兴趣的朋友看一看。

CharacterSet源码

文章整理:西部数码--专业提供域名注册虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!