ASP.NET抓取页面并分析页面数据的研究与探讨。
因项目(PORTAL)需求, 需要抓取特定页面数据,
刚做完, 贴出来与大家分享、探讨。。。希望可以得到支持与优化。
不多说 开正题:
因项目(PORTAL)需求, 需要抓取特定页面数据, 也就是抓去某个页面新闻标题和链接 放到portal上来
刚做完, 贴出来与大家分享、探讨。。。
不多说 开正题:
用的是Visual Studio 2010,下面是抓取页面并分析数据 的代码
if (!IsPostBack) { //////教学新闻 try { WebRequest request = WebRequest.Create("http://www.siva.edu.cn/renda/node6813/node6866/node6875/node7139/node7161/index.html"); WebResponse response = request.GetResponse(); StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("gb2312")); string tmp = reader.ReadToEnd().ToLower(); //提取数据约束:必须有“教学新闻”字样 和 · 号 tmp = tmp.Remove(0, tmp.IndexOf("教学新闻")); tmp = tmp.Replace(@"<tr>", "`"); //替换掉html字符为siva 为httphandler 转向提供便捷 tmp = tmp.Replace("html", "siva"); string[] str = tmp.Split('`'); List<string> list = new List<string>(); foreach (string a in str) { if (a.Contains("·")) { list.Add("<tr>" + a); } } //save list and last item must to drop UserInfoHandle ui = new UserInfoHandle(); for (int i = 0; i < list.Count-1; i++) { SivaNewsListEntity se = new SivaNewsListEntity(); se.Content = list[i]; se.CreateTime = System.DateTime.Now; se.NewsType = 1; ui.InsertSivaNewsList(se); } reader.Close(); reader.Dispose(); response.Close(); } catch (Exception ex) { Response.Write( ex.Message); }
提取出来的数据样例:
<tr> <td class="12n3">·<a href=/renda/node6813/node6866/node6875/node7139/node7161/node7163/u1a1661552.siva target=_blank class="12n3">我校在市级教学成果奖评选中荣获三等奖</a></td> </tr> <tr> <td class="12n3">·<a href=/renda/node6813/node6866/node6875/node7139/node7161/node7163/u1a1626696.siva target=_blank class="12n3">我校开展首届青年教师讲课比赛活动</a></td> </tr> <tr> <td class="12n3">·<a href=/renda/node6813/node6866/node6875/node7139/node7161/node7163/userobject1ai1574049.siva target=_blank class="12n3">教务处召开学期期末教学工作恳谈会</a></td> </tr> <tr> <td class="12n3">·<a href=http://61.129.89.229/down/siva/dzzc1114.doc target=_blank class="12n3">关于普通高等学校学生学年电子注册的通知</a></td> </tr> <tr> <td class="12n3">·<a href=/renda/node6813/node6866/node6875/node7139/node7161/node7163/u1a1546226.siva target=_blank class="12n3">高校学生获得学籍及毕业证书政策告知 </a></td> </tr> <tr> <td valign="top" class="14n1"><font color="#ff0000"><strong>·常用链接</strong></font></td> </tr> <tr> <td class="12n3">·<a href=/renda/node6813/node6866/node6875/node7139/node7161/node7163/u1a1661552.siva target=_blank class="12n3">我校在市级教学成果奖评选中荣获三等奖</a></td> </tr> <tr> <td class="12n3">·<a href=/renda/node6813/node6866/node6875/node7139/node7161/node7163/u1a1626696.siva target=_blank class="12n3">我校开展首届青年教师讲课比赛活动</a></td> </tr>
--------------------------------这里当时考虑 转化成XML 直接通过XLINQ读取XML数据,但感觉这样做风险很大。 万一页面代码不规范,转化成XML很容易就exception了
所以考虑 直接用一个<tr></tr>做为一个整体保存下来;
然后通过htppHandler URL转向。 OK
看代码:
namespace Handlers { public class IISHandlerSivaNews : IHttpHandler { /// <summary> /// You will need to configure this handler in the web.config file of your /// web and register it with IIS before being able to use it. For more information /// see the following link: http://go.microsoft.com/?linkid=8101007 /// </summary> #region IHttpHandler Members public bool IsReusable { // Return false in case your Managed Handler cannot be reused for another request. // Usually this would be false in case you have some state information preserved per request. get { return true; } } public void ProcessRequest(HttpContext context) { string urlStr = context.Request.RawUrl; //write your handler implementation here. urlStr = urlStr.Replace("siva", "html"); context.Response.Redirect("http://www.siva.edu.cn" + urlStr); //context.Response.Write("urlStr"); } #endregion } }
//////handler 配置
//意思是 所有siva后缀名的都调用IISHandlerSivaNews Handler
<httpHandlers>
<add verb="*" path="*.siva" type="Handlers.IISHandlerSivaNews,FDPortal.BLL"/>
</httpHandlers>
OK了 页面抓去数据搞定。。。。
欢迎讨论、优化。