[转载]C#文章抓取(含源码下载)
- C#
- 2011-08-08
- 85热度
- 0评论
[转载]文章抓取(含源码下载) - sharpCode - 博客园.
最近都左做一些资源采集的工作,比如采集新闻,flash,图片等,下面我们通过一个小例子,来详细的说明一下我采集资源的步骤,希望各位能提点建议,不胜感激。
下面就开始吧!我们这次要采集的是这个少儿英语动画故事 http://www.ebigear.com/reslist-92-1.html,虽然只有6页,但是已经对与这个例子来说已经足够了。
1.首先当然是要下载这个网页。
[csharp]
string seed = "http://www.ebigear.com/reslist-92-1.html";
HttpWebRequest wRequest=(HttpWebRequest)HttpWebRequest.Create(seed);
WebResponse wResponse = wRequest.GetResponse();
Stream stream = wResponse.GetResponseStream();
StreamReader streamReader = new StreamReader(stream);
string html = streamReader.ReadToEnd();
[/csharp]
得到下面的字符串
[html]
少儿英语动画故事-英语听力-list-大耳朵英语 - 免费在线英语学习 口语练习 四级听力资料 在线翻译 网络课堂 英语社区
-
A Country Road(中英文对照) 英文:A Country RoadVery far away from the city lived a poor ...
-
英文:A Hot Air BalloonIf I had a hot air balloon,I would fly around the world and see ...
-
A Lion and a mouse One fine day in spring, a mouse came out to the lawn and enjoyed the...
-
感谢大耳朵网友"大耳朵usa"提供的听力原文A Quiet QuizPlease, be quiet. This is a quiz.Q...
-
A wolf and his shadow 狼和它的影子When it was getting dark a wolf was walking along the road. ...
-
英文:Allen’s FearAllen does not like high places.Allen would never climb a ladder....
-
感谢大耳朵网友"dingxiang6"提供的听力原文Who is it ? A kangaroo nurse. She works all day. ...
-
Annie the Detective1.Annie and her family were enjoying dessert. Everyone loved Mother’s...
-
英文:At NightThe horse sleeps in the stable.The pig sleeps in the pen.The squirrel...
-
Autumn’s ComingIt’s a autumn,it’s fall,can you hear the call?The leaves are falling,...
-
英文:Ball Goes on a PicnicIt’s a nice fall day.So today, Ball is going on a picnic....
-
...
-
英文:Bob Goes to the DentistToday is my first dentist appointment.I really do not wa...
-
感谢大耳朵网友"大耳朵usa"提供的听力原文 Bremen Town Musicians Once upon a time, a donkey ...
![]() |
浏览记录 | ![]() |
- 您没有浏览历史
- 您没有浏览历史
![]() |
推荐资源 | ![]() |
![]() |
每天学英语 | ![]() |
更多 |
![]() |
最新社区精华帖子 | ![]() |
更多>> |
-
16:36
-
16:36
-
10:51
-
10:51
-
11:14
-
11:14
-
09:53
-
21:26
-
09:50
-
14:28
![]() |
经典学习方法 | ![]() |
更多>> |
听力排行
试题
视听
歌曲
电影
初中中考模拟训练五44-45
2006年高考听力模拟0202
四级试点考试听力新题型18
初中中考模拟训练一08
2003年北京西城区中考听力16-20
四级试点考试听力新题型29-31
初中英语长对话理解
2006年高考听力模拟0317-20
2005年全国卷高考英语听力10-12
《新概念英语》(美音)III-58
初中英语词汇初一(字母)上 a-h
新东方美国口语 01
新概念英语第二册13-01
高二英语第三学期听力U01 Words and expressions
第十届21世纪杯全国英语演讲比赛冠军——夏鹏
新概念英语第一册578h版09-3
《新概念英语》(美音)I-115
《新概念英语》(美音)II-64
《新概念英语》(美音)IV-04
Lady GaGa -Bad Romance
sophie zelmani -- I can't change
015插曲interlude - timi yuro
周华健 - 萍水相逢 -英文版
Greatest-Lady GaGa
Hayley Sales - more than you know
法语歌曲 Deux pieds
Don’t U Walk Away-Kay B
英文歌曲欣赏 casablanca - bertie higgins
经典英文歌曲收藏take me home,country road
英文爱情片傲慢与偏见 A
动画学英语:芭比公主之梦幻仙境节选
第一段susan的婚礼
英文爱情片简.爱 A
英文爱情片乱世佳人 A
《料理鼠王》 讲解5
难以忽略的真相2
英文爱情片鬼马小精灵 1
英文爱情片傲慢与偏见 B
英文爱情片乱世佳人 D
![]() |
听力资料目录导航 | ![]() |
站务客服QQ群:19012993 联系客服 站长信箱:ebigear@gmail.com 英语交流QQ群 捐助贫困儿童 |
| 免责声明:本站只提供资源播放平台,如果站内部分资源侵犯您的权益,请您告知,站长会立即处理。 |
| Copyright © 2003-2011 大耳朵英语 京ICP备10010568号 |
大耳朵在线聊天
[/html]
2. 我们来分析一下要抓取的文章的链接
[html]
国外英语动画故事-A Country Road
国外英语动画故事-A Hot Air Balloon
[/html]
经过分析,可以得到 获取链接的正则表达式
/res-92[^\"]+html
得到了我们所需要的链接,如下图

3.得到了所需要的链接,接下来我们就可以进行下一步了。
[csharp]
Regex rg1 = new Regex("/res-92[^\"]+html", RegexOptions.Compiled |RegexOptions.IgnoreCase);
MatchCollection mc=rg1.Matches(html);
foreach (Match a in mc)
{
/*在这里处理每一个链接*/
string catchUrl = @"http://www.ebigear.com" + a.Value;
wRequest = (HttpWebRequest)HttpWebRequest.Create(catchUrl);
wResponse = wRequest.GetResponse();
stream = wResponse.GetResponseStream();
streamReader = new StreamReader(stream);
html = streamReader.ReadToEnd(); //跟之前那样先获取该网页的源代码
Regex rgN = new Regex("\n|\r|\t");
Regex rgReplace = new Regex(""");
Regex rgContent = new Regex("(?<=
).*?(?=
)",RegexOptions.IgnoreCase|RegexOptions.Multiline);
//上面是获取当前网页中文章的内容的正则。
ShowBox.Text+= rgContent.Match(rgReplace.Replace( rgN.Replace(html,""),"\"")).Value+"\n\n\n\n\n"; //要先去掉\n\t\r,不然正则没效果
}
[/csharp]
4.结果展示

5.后期格式处理。
这里就不处理了,把<br/> 替换一下就可以了。
就到这里吧,谢谢,睡觉去了。











