惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

F
Fortinet All Blogs
云风的 BLOG
云风的 BLOG
M
MIT News - Artificial intelligence
WordPress大学
WordPress大学
T
Tailwind CSS Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
S
Secure Thoughts
博客园 - 【当耐特】
Know Your Adversary
Know Your Adversary
NISL@THU
NISL@THU
博客园 - 司徒正美
Last Week in AI
Last Week in AI
C
Cybersecurity and Infrastructure Security Agency CISA
P
Privacy & Cybersecurity Law Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
B
Blog
The GitHub Blog
The GitHub Blog
小众软件
小众软件
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Spread Privacy
Spread Privacy
Martin Fowler
Martin Fowler
博客园 - 叶小钗
Security Archives - TechRepublic
Security Archives - TechRepublic
T
Tenable Blog
S
Securelist
博客园 - 三生石上(FineUI控件)
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Microsoft Security Blog
Microsoft Security Blog
Apple Machine Learning Research
Apple Machine Learning Research
罗磊的独立博客
T
Threat Research - Cisco Blogs
Application and Cybersecurity Blog
Application and Cybersecurity Blog
F
Full Disclosure
Cloudbric
Cloudbric
The Cloudflare Blog
Y
Y Combinator Blog
Hugging Face - Blog
Hugging Face - Blog
Microsoft Azure Blog
Microsoft Azure Blog
H
Hacker News: Front Page
腾讯CDC
L
Lohrmann on Cybersecurity
C
CERT Recently Published Vulnerability Notes
V2EX - 技术
V2EX - 技术
GbyAI
GbyAI
TaoSecurity Blog
TaoSecurity Blog
I
Intezer
The Last Watchdog
The Last Watchdog
G
GRAHAM CLULEY
Google Online Security Blog
Google Online Security Blog
T
The Blog of Author Tim Ferriss

博客园 - chinachen

查看DLL 版本 平台 x86 x64 MS SQL2008 数据库备份(收集) android 使用sqlite几种方式。 C# 接口 索引 属性 实质及应用 (收集) get set 用法总结 (收集) MVC 资料收集(一) windows运行命令大全 Html:meta标签的奥妙 (收集) - chinachen - 博客园 C#中indexof和substring函数用法 (收集) - chinachen - 博客园 BlogEngine 深入研究(一) 开篇 ASP.NET实现回调服务器方法 (收集) 统一过程,敏捷过程,微软过程(收集) HTTP服务器状态代码定义(Status Code Definitions)(收集) Lambda 表达式 源自MSDN 多个表 导出EXCEL 代码 (分享) C# 参考之方法参数关键字:params、ref及out (收集) nchar,char,varchar 与nvarchar区别 (收集) - chinachen 抽象类与接口区别 (收集) virtual 与 Abstract 区别
ASP.NET抓取页面并分析页面数据的研究与探讨。
chinachen · 2010-04-01 · via 博客园 - chinachen

因项目(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了   页面抓去数据搞定。。。。

欢迎讨论、优化。