下面是代码,代码没有问题 读别的页 一般没有问题,但是读带有百度百科的 到了百科就中断, 就返回字符串了。不是完整的html代码。只有一部分;不知道怎么处理了。。
请各位帮帮忙,困扰好久了。//调用
GetHtml("http://www.soso.com/q?pid=s.idx&cid=s.idx.se&w=%D1%B5%C1%B7%CB%FE"); public string GetHtml(string url)
{
//判断网页编码
Encoding wCode;
string PostPara = "";
CookieContainer CookieCon = new CookieContainer();
HttpWebRequest wReq;
wReq = (HttpWebRequest)WebRequest.Create(@url);
wReq.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.0; .NET CLR 1.1.4322; .NET CLR 2.0.50215;)";
Match a = Regex.Match(url, @"(http://).[^/]*[?=/]", RegexOptions.IgnoreCase);
string url1 = a.Groups[0].Value.ToString();
wReq.Referer = url1;
wReq.Method = "GET";
//设置页面超时时间为12秒
wReq.Timeout = 12000;
HttpWebResponse wResp = (HttpWebResponse)wReq.GetResponse();
System.IO.Stream respStream = wResp.GetResponseStream();
string strWebData = "";
try
{ string cType = wResp.ContentType.ToLower();
Match charSetMatch = Regex.Match(cType, "(?<=charset=)([^<]*)*", RegexOptions.IgnoreCase | RegexOptions.Multiline);
string webCharSet = charSetMatch.ToString();
wCode = System.Text.Encoding.GetEncoding(webCharSet);
}
catch
{
wCode = Encoding.Default;
}
if (wResp.ContentEncoding == "gzip")
{
GZipStream myGZip = new GZipStream(respStream, CompressionMode.Decompress);
System.IO.StreamReader reader;
reader = new System.IO.StreamReader(myGZip, wCode);
strWebData = reader.ReadToEnd();
reader.Close();
reader.Dispose();
}
else
{
System.IO.StreamReader reader;
reader = new System.IO.StreamReader(respStream, wCode);
strWebData = reader.ReadToEnd();
reader.Close();
reader.Dispose();
}
// this.m_WebpageSource = strWebData;
return strWebData; }
请各位帮帮忙,困扰好久了。//调用
GetHtml("http://www.soso.com/q?pid=s.idx&cid=s.idx.se&w=%D1%B5%C1%B7%CB%FE"); public string GetHtml(string url)
{
//判断网页编码
Encoding wCode;
string PostPara = "";
CookieContainer CookieCon = new CookieContainer();
HttpWebRequest wReq;
wReq = (HttpWebRequest)WebRequest.Create(@url);
wReq.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.0; .NET CLR 1.1.4322; .NET CLR 2.0.50215;)";
Match a = Regex.Match(url, @"(http://).[^/]*[?=/]", RegexOptions.IgnoreCase);
string url1 = a.Groups[0].Value.ToString();
wReq.Referer = url1;
wReq.Method = "GET";
//设置页面超时时间为12秒
wReq.Timeout = 12000;
HttpWebResponse wResp = (HttpWebResponse)wReq.GetResponse();
System.IO.Stream respStream = wResp.GetResponseStream();
string strWebData = "";
try
{ string cType = wResp.ContentType.ToLower();
Match charSetMatch = Regex.Match(cType, "(?<=charset=)([^<]*)*", RegexOptions.IgnoreCase | RegexOptions.Multiline);
string webCharSet = charSetMatch.ToString();
wCode = System.Text.Encoding.GetEncoding(webCharSet);
}
catch
{
wCode = Encoding.Default;
}
if (wResp.ContentEncoding == "gzip")
{
GZipStream myGZip = new GZipStream(respStream, CompressionMode.Decompress);
System.IO.StreamReader reader;
reader = new System.IO.StreamReader(myGZip, wCode);
strWebData = reader.ReadToEnd();
reader.Close();
reader.Dispose();
}
else
{
System.IO.StreamReader reader;
reader = new System.IO.StreamReader(respStream, wCode);
strWebData = reader.ReadToEnd();
reader.Close();
reader.Dispose();
}
// this.m_WebpageSource = strWebData;
return strWebData; }
解决方案 »
- 用Javascript实现把页面上所有文本框的值都赋成Hello
- C#windows form 图片怎么保存到assess里面
- 非常非常奇怪的问题?
- C# 插件问题
- 请问vs2005如何设置编译版本号自增?
- vs2005菜鸟问题,大哥们快来抢分吧
- 请问我的在debug文件夹下的C#应用程序配置文件每次程序重新编译完后就不见了,要让每次编译后还有怎么办!!!!谢谢了.
- 如何用SQL服务器的域名连接数据库
- 关于数据读取问题
- 我在wpf的webbrowser上潜入了一张谷歌地图的网页,请问如何能够外部获取网页上的经纬度?
- c#winform 中gridcontrol绑定数据时怎样换行
- 强类型数据中的ROW怎么 new ?
public string GetHtml(string url)
{
return GetHtml(url, null);
}
// 获取网页的HTML内容,指定Encoding
public string GetHtml(string url, Encoding encoding)
{
byte[] buf = new WebClient().DownloadData(url);
if (encoding != null) return encoding.GetString(buf);
string html = Encoding.UTF8.GetString(buf);
encoding = GetEncoding(html);
if (encoding == null || encoding == Encoding.UTF8) return html;
return encoding.GetString(buf);
}
// 根据网页的HTML内容提取网页的Encoding
public Encoding GetEncoding(string html)
{
string pattern = @"(?i)\bcharset=(?<charset>[-a-zA-Z_0-9]+)";
string charset = Regex.Match(html, pattern).Groups["charset"].Value;
try
{
return Encoding.GetEncoding(charset);
}
catch (ArgumentException)
{
return null;
}
}
//以上方法应该是目前最优的
GetHtml("http://www.soso.com/q?pid=s.idx&cid=s.idx.se&w=%D1%B5%C1%B7%CB%FE");
或者用文本可视化工具看一下。就只有一部分呀????
StreamReader 就认为读完了。但是有点页面什么也没有,看着连个空格都没有,读到百科照样的停掉了
StreamReader 遇到特殊符号就 停了,看看有什么过滤的方法或者
什么办法让我把搜搜的排名 获取到就行了
要是都得到了,我就可以查到了。
现在上面的办法只能是 得到百度百科上面的代码,要是 这个页面上没有百度百科 完全没有问题
但是一般的都有,就是得不到后面的代码。所以没有办法获得排名 只能用神器了 正则