采集到的html代码不完整,是bug吗?

米娜Rose 2010-07-27 02:29:55
采集到的html代码不完整,是bug吗?

还是说我的采集规则不正确呢?

并且采集出来的中文是乱码。

这是什么问题呢?
...全文
322 24 打赏 收藏 转发到动态 举报
写回复
用AI写文章
24 条回复
切换为时间正序
请发表友善的回复…
发表回复
nitaiyoucala 2010-07-27
  • 打赏
  • 举报
回复
路过,
amandag 2010-07-27
  • 打赏
  • 举报
回复
[Quote=引用 21 楼 wuyazhe 的回复:]
你非常罕见非常罕见的回复过一个正则问题。。。
[/Quote]

呵呵,好像是的。

你和过客都没回复,我就献丑了
amandag 2010-07-27
  • 打赏
  • 举报
回复
//5、可以实现简单的表单提交及cookie保存
模拟表单提交的时候 request.Method = "post";
使用的cookie就用刚才获得的cookie
兔子-顾问 2010-07-27
  • 打赏
  • 举报
回复
[Quote=引用 20 楼 amandag 的回复:]
引用 16 楼 wuyazhe 的回复:
引用 9 楼 amandag 的回复:

楼主,我贴的就是高歌写的。原版来给你解释了。


好奇怪,你从哪里来的我的代码
[/Quote]

你非常罕见非常罕见的回复过一个正则问题。。。
amandag 2010-07-27
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 wuyazhe 的回复:]
引用 9 楼 amandag 的回复:

楼主,我贴的就是高歌写的。原版来给你解释了。
[/Quote]

好奇怪,你从哪里来的我的代码
ybfqzm 2010-07-27
  • 打赏
  • 举报
回复
什么什么啊!
米娜Rose 2010-07-27
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 wuyazhe 的回复:]
引用 9 楼 amandag 的回复:

楼主,我贴的就是高歌写的。原版来给你解释了。
[/Quote]
呵呵,这位大哥厉害。小妹还需要多学习学习啊!
兔子-顾问 2010-07-27
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 amandag 的回复:]
[/Quote]
楼主,我贴的就是高歌写的。原版来给你解释了。
米娜Rose 2010-07-27
  • 打赏
  • 举报
回复
各位大哥帮帮忙,能否贴个代码?不胜感激!
米娜Rose 2010-07-27
  • 打赏
  • 举报
回复
没人知道4楼的第5步代码吗?
szldk 2010-07-27
  • 打赏
  • 举报
回复
路过,
米娜Rose 2010-07-27
  • 打赏
  • 举报
回复
现在已经把html全部采集到了,问下如题第5步该怎么实现?
米娜Rose 2010-07-27
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 amandag 的回复:]
这位大哥的编码没有乱了,但每个网站的编码格式都不一样啊,怎样通用所有的网站呢?
==
这个不可能的,你可以给个xml文件,里面存放了每个网站的相关配置信息,采集的时候读这个配置文件即可

比如

XML code
<Media>
<ClassName>xxx</ClassName>
<Name>xxx</Name>
<Si……
[/Quote]

恩恩,谢谢,那网站采集不完整是什么问题呢?
amandag 2010-07-27
  • 打赏
  • 举报
回复
这位大哥的编码没有乱了,但每个网站的编码格式都不一样啊,怎样通用所有的网站呢?
==
这个不可能的,你可以给个xml文件,里面存放了每个网站的相关配置信息,采集的时候读这个配置文件即可

比如
    <Media>
<ClassName>xxx</ClassName>
<Name>xxx</Name>
<Site>http://www.xxx.com</Site>
<Encoding>UTF-8</Encoding>
<Enable>true</Enable>
<IsGatherAllData>false</IsGatherAllData>
<GatherPageCount>10</GatherPageCount>
<DelayMilliSeconds>0</DelayMilliSeconds>
米娜Rose 2010-07-27
  • 打赏
  • 举报
回复
而且采集到的html代码不完整啊,只有头部部分采集到了。
米娜Rose 2010-07-27
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 wuyazhe 的回复:]
C# code

static class WebFunc
{
private static CookieContainer cookie = new CookieContainer();
private static string contentType = "application/x-www-form-urlencoded";
private static……
[/Quote]
这位大哥的编码没有乱了,但每个网站的编码格式都不一样啊,怎样通用所有的网站呢?
兔子-顾问 2010-07-27
  • 打赏
  • 举报
回复

static class WebFunc
{
private static CookieContainer cookie = new CookieContainer();
private static string contentType = "application/x-www-form-urlencoded";
private static string accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/x-silverlight, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, application/x-silverlight-2-b1, */*";
private static string userAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)";

public static string GetHtmlEx(string url, Encoding encoding)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.UserAgent = userAgent;
request.ContentType = contentType;
request.CookieContainer = cookie;
request.Accept = accept;
request.Method = "get";

WebResponse response = request.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream, encoding);
String html = reader.ReadToEnd();
response.Close();

return html;
}
}

//调用
WebFunc.GetHtmlEx("网址", Encoding.UTF8)

米娜Rose 2010-07-27
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 zhengfujie 的回复:]
不完整。采集规则有问题吧。
乱码。就是编码的问题咯
[/Quote]

我的代码编码都有转换啊,但是好象没有什么作用?
米娜Rose 2010-07-27
  • 打赏
  • 举报
回复

//1、提取网页的纯文本
HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create("http://community.csdn.net/");
myReq.Accept = "Accept-Language:zh-cn";
myReq.Referer = "http://quote.morningstar.com/fund/f.aspx?Country=USA&Symbol=AGDA";
myReq.MaximumAutomaticRedirections = 1;
myReq.AllowAutoRedirect = true;
HttpWebResponse myres = (HttpWebResponse)myReq.GetResponse();//
Stream resStream = myres.GetResponseStream();
StreamReader sr = new StreamReader(resStream, System.Text.Encoding.GetEncoding("GB2312"));

char[] buff = new char[20000];
int c = 0; //实际读取的字节数
while ((c = sr.Read(buff, 0, buff.Length)) > 0)//将抓到的代码逐步放到数组里面。
{
Response.Write(buff, 0, c);
}

//2、去所有html标签和javascript代码
//html为得到的html纯文本
string Htmlstring = "";
foreach (char c1 in buff)
{
Htmlstring += c1.ToString();
}

//删除脚本
Htmlstring = Regex.Replace(Htmlstring, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase);
//删除HTML
Htmlstring = Regex.Replace(Htmlstring, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"([\r\n])[\s]+", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"-->", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"<!--.*", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(quot|#34);", "\"", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(amp|#38);", "&", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(lt|#60);", "<", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(gt|#62);", ">", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(nbsp|#160);", " ", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(iexcl|#161);", "\xa1", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(cent|#162);", "\xa2", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(pound|#163);", "\xa3", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(copy|#169);", "\xa9", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&#(\d+);", "", RegexOptions.IgnoreCase);
// add new 2006-12-30
Htmlstring = Regex.Replace(Htmlstring, @"<.*?>", "", RegexOptions.IgnoreCase);// 清除所有标签
Htmlstring = Regex.Replace(Htmlstring, @"<script.*>[\s\S]*?</script>", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"<td.*?>", "", RegexOptions.IgnoreCase);// 清除td
Htmlstring = Regex.Replace(Htmlstring, @"</td>", "", RegexOptions.IgnoreCase);// 清除td
Htmlstring = Regex.Replace(Htmlstring, @"<div.*?>", "", RegexOptions.IgnoreCase);// 清除div
Htmlstring = Regex.Replace(Htmlstring, @"</div>", "", RegexOptions.IgnoreCase);// 清除td

Htmlstring.Replace("<", "");
Htmlstring.Replace(">", "");
Htmlstring.Replace("\r\n", "");
Htmlstring = HttpContext.Current.Server.HtmlEncode(Htmlstring).Trim();
string clearScriptPattern = @"]*>(.|n)*?";//前面去掉空格,中间(.|n)*?为非贪婪匹配
string clearStylePattern = @"(.|n)*?";
string clearHtmlPattern = @"<[^>]*>";
string clearSpacePattern = @" | |s";
RegexOptions options = RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.Compiled;

string parseResult = Regex.Replace(Htmlstring, clearScriptPattern, "", options);
parseResult = Regex.Replace(parseResult, clearStylePattern, "", options);
parseResult = Regex.Replace(parseResult, clearHtmlPattern, "", options);
parseResult = Regex.Replace(parseResult, clearSpacePattern, " ", options);

//3、提取网页的链接,包括href和frame及iframe
//Get_url_Array(userinput, webtext);

//4、提取网页的title等(其它的标签可依此类推,正则是一样的)
string url = "http://www.cftea.com/images/logo.gif";
WebClient wc = new WebClient();
wc.Credentials = CredentialCache.DefaultCredentials;
Byte[] pages = wc.DownloadData(url);
string pageHtml = Encoding.GetEncoding("GB2312").GetString(pages);
Match charSetMatch = Regex.Match(pageHtml, "<meta([^<]*)charset=([^<]*)\"", RegexOptions.IgnoreCase | RegexOptions.Multiline);
string getcode = charSetMatch.Groups[2].Value;
pageHtml = Encoding.GetEncoding(getcode).GetString(pages);
Match title = Regex.Match(pageHtml, "<title>(.*)</title>");
this.NE_Title.Text = title.Groups[1].Value.ToString();//获得标题
string meatregex = "<meta" + @"\s+" + "name=\"description\"" + @"\s+" + "content=\"(?<content>[^\"" + @"\<\>" + "]*)\"";
Match description = Regex.Match(pageHtml, meatregex, RegexOptions.IgnoreCase | RegexOptions.Multiline);
Label1.Text = description.Groups[1].Value;


//5、可以实现简单的表单提交及cookie保存

myhope88 2010-07-27
  • 打赏
  • 举报
回复
如果采集的时候有应用过滤或者正则的话,那应该要检查下过滤或正则有没有正确
加载更多回复(2)

62,051

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧