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

推荐订阅源

Y
Y Combinator Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
WordPress大学
WordPress大学
T
Threatpost
K
Kaspersky official blog
Cisco Talos Blog
Cisco Talos Blog
博客园 - 三生石上(FineUI控件)
Vercel News
Vercel News
T
Tenable Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
L
LINUX DO - 热门话题
Scott Helme
Scott Helme
云风的 BLOG
云风的 BLOG
博客园 - Franky
Webroot Blog
Webroot Blog
D
DataBreaches.Net
Stack Overflow Blog
Stack Overflow Blog
MongoDB | Blog
MongoDB | Blog
H
Help Net Security
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
F
Fortinet All Blogs
PCI Perspectives
PCI Perspectives
Google DeepMind News
Google DeepMind News
Microsoft Azure Blog
Microsoft Azure Blog
Blog — PlanetScale
Blog — PlanetScale
P
Proofpoint News Feed
Google DeepMind News
Google DeepMind News
月光博客
月光博客
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Help Net Security
Help Net Security
T
The Blog of Author Tim Ferriss
T
Tailwind CSS Blog
Hugging Face - Blog
Hugging Face - Blog
GbyAI
GbyAI
O
OpenAI News
Martin Fowler
Martin Fowler
W
WeLiveSecurity
C
Cyber Attacks, Cyber Crime and Cyber Security
Project Zero
Project Zero
V
Vulnerabilities – Threatpost
L
LangChain Blog
P
Privacy & Cybersecurity Law Blog
S
Security Affairs
P
Privacy International News Feed
C
Cybersecurity and Infrastructure Security Agency CISA
MyScale Blog
MyScale Blog
NISL@THU
NISL@THU
T
Troy Hunt's Blog
C
Cisco Blogs
IT之家
IT之家

博客园 - 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了   页面抓去数据搞定。。。。

欢迎讨论、优化。