ITPub博客

首页 > Linux操作系统 > Linux操作系统 > ASP.NET 2.0 网页采集方法

ASP.NET 2.0 网页采集方法

原创 Linux操作系统 作者:iDotNetSpace 时间:2008-09-24 13:34:24 0 删除 编辑

 

<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--> 1     public string GetRegValue(string HtmlCode, string RegexString, string GroupKey, bool RightToLeft)
 2     {
 3         MatchCollection m;
 4         Regex r;
 5         if (RightToLeft == true)
 6         {
 7             r = new Regex(RegexString, RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.RightToLeft);
 8         }
 9         else
10         {
11             r = new Regex(RegexString, RegexOptions.IgnoreCase | RegexOptions.Singleline);
12         }
13         m = r.Matches(HtmlCode);
14         string[] MatchValue = new string[m.Count];
15         for (int i = 0; i < m.Count; i++)
16         {
17             MatchValue[i] = m[i].Groups[GroupKey].Value;
18         }
19         if (MatchValue.Length > 0)
20         {
21             return MatchValue[0].ToString().Trim();
22         }
23         else
24         {
25             return "";
26         }
27     }
28     public string SniffwebCodeReturnList(string code, string wordsBegin, string wordsEnd)
29     {
30         try
31         {
32             ArrayList urlList = new ArrayList();
33             //string NewsTitle = "";
34             Regex regex1 = new Regex("" + wordsBegin + @"(?[\s\S]+?)</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #000000">+</SPAN><SPAN style="COLOR: #000000"> wordsEnd </SPAN><SPAN style="COLOR: #000000">+</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #800000">""</SPAN><SPAN style="COLOR: #000000">, RegexOptions.Compiled </SPAN><SPAN style="COLOR: #000000">|</SPAN><SPAN style="COLOR: #000000"> RegexOptions.IgnoreCase);<BR></SPAN><SPAN style="COLOR: #008080">35</SPAN> <SPAN style="COLOR: #000000">            </SPAN><SPAN style="COLOR: #0000ff">for</SPAN><SPAN style="COLOR: #000000"> (Match match1 </SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> regex1.Match(code); match1.Success; match1 </SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> match1.NextMatch())<BR></SPAN><SPAN style="COLOR: #008080">36</SPAN> <SPAN style="COLOR: #000000">            {<BR></SPAN><SPAN style="COLOR: #008080">37</SPAN> <SPAN style="COLOR: #000000">                urlList.Add(match1.Groups[</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #800000">title</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #000000">].ToString());<BR></SPAN><SPAN style="COLOR: #008080">38</SPAN> <SPAN style="COLOR: #000000">            }<BR></SPAN><SPAN style="COLOR: #008080">39</SPAN> <SPAN style="COLOR: #000000">            </SPAN><SPAN style="COLOR: #0000ff">if</SPAN><SPAN style="COLOR: #000000"> (urlList.Count </SPAN><SPAN style="COLOR: #000000">></SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #800080">0</SPAN><SPAN style="COLOR: #000000">)<BR></SPAN><SPAN style="COLOR: #008080">40</SPAN> <SPAN style="COLOR: #000000">            {<BR></SPAN><SPAN style="COLOR: #008080">41</SPAN> <SPAN style="COLOR: #000000">                </SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000"> urlList[</SPAN><SPAN style="COLOR: #800080">0</SPAN><SPAN style="COLOR: #000000">].ToString();<BR></SPAN><SPAN style="COLOR: #008080">42</SPAN> <SPAN style="COLOR: #000000">            }<BR></SPAN><SPAN style="COLOR: #008080">43</SPAN> <SPAN style="COLOR: #000000">            </SPAN><SPAN style="COLOR: #0000ff">else</SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">44</SPAN> <SPAN style="COLOR: #000000">            {<BR></SPAN><SPAN style="COLOR: #008080">45</SPAN> <SPAN style="COLOR: #000000">                </SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #800000">""</SPAN><SPAN style="COLOR: #000000">;<BR></SPAN><SPAN style="COLOR: #008080">46</SPAN> <SPAN style="COLOR: #000000">            }<BR></SPAN><SPAN style="COLOR: #008080">47</SPAN> <SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">48</SPAN> <SPAN style="COLOR: #000000">        }<BR></SPAN><SPAN style="COLOR: #008080">49</SPAN> <SPAN style="COLOR: #000000">        </SPAN><SPAN style="COLOR: #0000ff">catch</SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">50</SPAN> <SPAN style="COLOR: #000000">        {<BR></SPAN><SPAN style="COLOR: #008080">51</SPAN> <SPAN style="COLOR: #000000">            </SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #800000">""</SPAN><SPAN style="COLOR: #000000">;<BR></SPAN><SPAN style="COLOR: #008080">52</SPAN> <SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">53</SPAN> <SPAN style="COLOR: #000000">        }<BR></SPAN><SPAN style="COLOR: #008080">54</SPAN> <SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">55</SPAN> <SPAN style="COLOR: #000000">    }</SPAN></DIV></DIV> </div> <p style="clear:both;"></p> <p class="translate"> 来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/12639172/viewspace-462852/ ,如需转载,请注明出处,否则将追究法律责任。 </p> <!-- --> </div> </div> <div class="preview-footer clearfix"> <!--已点赞、收藏--> <div class="icon-praise" id="praise" onclick="blogObj.praise($(this),'/praise/')"><span class="text"></span><span class="num">0</span></div> <div class="icon-collect" id="collect" onclick="blogObj.praise($(this),'/favour/')"><span class="text"></span><span class="num">0</span></div> <div class="share-group fr"> <div class="bdsharebuttonbox bdshare-button-style0-16" data-bd-bind="1522216707615"> <span class="fl">分享到:</span> <a href="javascript:;" class="bds_tsina" data-cmd="tsina" title="分享到新浪微博"></a> <a href="javascript:;" class="bds_sqq" data-cmd="sqq" title="分享到QQ好友"></a> <a href="javascript:;" class="bds_qzone" data-cmd="qzone" title="分享到QQ空间"></a> <a href="javascript:;" class="bds_weixin" data-cmd="weixin" title="分享到微信"></a> </div> <script> window._bd_share_config = { "common": { "bdSnsKey": {}, "bdText": "", "bdDesc": "", "bdMini": "", "bdMiniList": false, "bdPic": "", "bdStyle": "0", "bdSize": "32" }, "share": {"bdSize": 16} }; with (document) 0[(getElementsByTagName('head')[0] || body).appendChild(createElement('script')).src = '/static/api/js/share.js?v=89860593.js?cdnversion=' + ~(-new Date() / 36e5)]; if ($(window).width() < 1320) { if ($(window).width() < 1200) { $(".sideslip").css({ "left": "20px", "margin-left": 0 }) } else { $(".sideslip").css({ "margin-left": "-590px" }) } }</script> </div> </div> <div class="article-context"> <div class="fl"> <span class="color77">上一篇:</span> <a href="https://blog.itpub.net/12639172/viewspace-462851/">ASP.NET 2.0 加密 Cookies</a> </div> <div class="fr"> <span class="color77">下一篇:</span> <a href="https://blog.itpub.net/12639172/viewspace-462855/">轻松掌握Ajax.net系列教程</a> </div> </div> <input type="hidden" id="blogId" name="blogId" value="462852"> <!--评论开始--> <div class="blog-comment"> <div class="new-comment"> <input type="hidden" id="hid" name="hid" value="462852"> <input type="hidden" name="_token" value="cTIOwt2QtszzqzNomSlGkdecD8xogkSBaLZUjO1s"> <a class="avatar" href="javascript:;"> <img src="/images/user_pic_default.png"></a> <!--用户未登录--> <div class="sign-container"> <span>请登录后发表评论</span> <a class="btn btn-sign"> <a class="layui-btn ml30" onclick="blog.login(event)">登录</a> </a> </div> <!--用户已登录--> <textarea placeholder="请写下你的评论…" onfocus="blogObj.textareaFocus($(this),200)" maxlength=200></textarea> </div> <div class="comment-list"> <div class="top-title"><span class="all-comment">全部评论</span> <span class="colorbb ml20"></span> </div> <div class="comment-items" id="comment_items"> <script type="text/template" id='blog_template'> <%for(var i=0;i <items.length;i++){%> <div class="comment-item"> <div class="author"> <a target="_blank" href="/<%=items[i].uid%>" class="avatar"> <img src="<%=items[i].headimg%>" alt=""> </a> <div class="info"> <div class="name"><a target="_blank" href="/<%=items[i].uid%>"><%=items[i].username%></a> <span class="fr reply-btn" onclick="blogObj.newComment($(this),'<%=items[i].username%>',<%=items[i].id%>)">回复</span> </div> <div class="time colorbb"><%=items[i].createtime%></div> </div> </div> <p class="comment-wrap"><%=items[i].content%></p> <%if(items[i].items.items.length) { %> <div class="sub-comment-list"> <%for(var j=0;j <items [i].items.items.length;j++){%> <div class="sub-comment"> <div class="color77 time-reply"> <span class="time"><%=items[i].items.items[j].createtime%></span> <span class="reply-btn fr" onclick="blogObj.newComment($(this),'<%=items[i].items.items[j].username%>',<%=items[i].items.items[j].id%>)">回复</span> </div> <p><a href="/<%=items[i].uid%>"><%=items[i].items.items[j].username%></a>   回复   <a href="/<%=items[i].touid%>"><%=items[i].items.items[j].tousername%></a>: <span class="content"><%=items[i].items.items[j].content%></span></p> </div> <%}%> <%if(items[i].items.total > 5) { %> <div class="more-comment"><span class="sub-comment-count">还有<i class="count"><%=items[i].items.total-5%></i>条评论</span><span class="more-comment-btn" onclick=blogObj.loadSubComment($(this),<%=items[i].id%>) data-count=1 data-flag=true>点击查看</span> </div> <%}%> </div> <%}%> </div> <%}%> </script> </div> </div> </div> <!--评论结束--> </div> <script> </script> <div class="fr w290"> <!--作者信息开始--> <!--作者信息开始--> <div class="author-info right-fixed "> <div class="head-img"> <a href="https://blog.itpub.net/12639172/"><img src="http://account.itpub.net/api/avatar.php?uid=12639172" alt=""></a> </div> <div class="author-name"><a href="https://blog.itpub.net/12639172/">iDotNetSpace</a></div> <div class="author-intro"> </div> <p class="register-time"><span class="color77">注册时间:</span>2008-01-04</p> <ul class="tree-list clearfix"> <li> <div class="item-tt">博文量</div> <a href="https://blog.itpub.net/12639172/"><span class="item-num blognum">2376</span></a> </li> <li> <div class="item-tt">访问量</div> <div class="item-num blogviewnum">5766042</div> </li> </ul> </div> <!--作者信息结束--> <script> var data = {uid: 12639172}; $._ajax({ url: '/getAuthorInfo/', data: data, type: 'get', dataType: 'json', success: function (data) { if (data.code == 200) { var data = data.data; $(".blognum").text(data.blognum); $(".blogviewnum").text(data.visitednum); } } }); </script> <!--作者信息结束--> <!--博文推荐开始--> <div class="blog-choice right-fixed"> <h3 class="choice-title">最新文章</h3> <ul class="newul"> <li class="new-item"> <a href="https://blog.itpub.net/12639172/viewspace-683059/" class="clearfix"> ASP.NET入门五步详解 </a> </li> <li class="new-item"> <a href="https://blog.itpub.net/12639172/viewspace-683057/" class="clearfix"> ASP.NET配置错误页面浅析 </a> </li> <li class="new-item"> <a href="https://blog.itpub.net/12639172/viewspace-683056/" class="clearfix"> 介绍ASP.NET控件ID </a> </li> <li class="new-item"> <a href="https://blog.itpub.net/12639172/viewspace-683055/" class="clearfix"> ASP.NET中批量删除--只调用一次存储过程 </a> </li> <li class="new-item"> <a href="https://blog.itpub.net/12639172/viewspace-683054/" class="clearfix"> C#后台调用前台javascript的五种方法 </a> </li> <li class="new-item"> <a href="https://blog.itpub.net/12639172/viewspace-683051/" class="clearfix"> 学习ASP.NET的流程 </a> </li> <li class="new-item"> <a href="https://blog.itpub.net/12639172/viewspace-683050/" class="clearfix"> 详解ASP.NET页面的aspx扩展 </a> </li> <li class="new-item"> <a href="https://blog.itpub.net/12639172/viewspace-683049/" class="clearfix"> asp.net中GridView导出数据 </a> </li> <li class="new-item"> <a href="https://blog.itpub.net/12639172/viewspace-683048/" class="clearfix"> ASP.NET中获取URL重写前的原始地址 </a> </li> <li class="new-item"> <a href="https://blog.itpub.net/12639172/viewspace-682721/" class="clearfix"> C#常用线程同步方法应用场景和实现原理 </a> </li> <ul> </div> <!--大牛精选结束--> </div> </div> <div class="icon-back-top" onclick="$('html,body').animate({scrollTop:0},'slow');"></div> <!--main部分结束--> <!--百度推送--> <script> (function () { var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })(); </script> <!--footer部分开始--> <div class="blog-footer"> <div class="w1200 pr"> <div class="footer-links"> <a class="icon-blog icon-wx mr30"> <div class="wx-qrcode"> <img src="/images/qrcode.jpg" alt=""> </div> </a> <a href="https://weibo.com/itpub2001?from=myfollow_all" class="icon-blog icon-wb" target="_blank"></a> </div> <p class="footer-nav"> <a href="http://www.it168.com/bottomfile/it168.shtml" target="_blank">支持我们</a> <a href="http://www.it168.com/bottomfile/tgzn.shtml" target="_blank">作者招募</a> <a href="http://www.it168.com/bottomfile/sytk.shtml" target="_blank">用户协议</a> <a href="//blog.itpub.net/31509949/viewspace-2157750/" target="_blank">FAQ</a> <a href="http://edu.itpub.net/contactus.html" target="_blank">Contact Us</a> <script src="https://s22.cnzz.com/z_stat.php?id=1274521965&web_id=1274521965" language="JavaScript"></script> </p> <p>北京盛拓优讯信息技术有限公司. 版权所有  <a style="color:#777777;" target="_blank" href="http://beian.miit.gov.cn">京ICP备16024965号-8</a> 北京市公安局海淀分局网监中心备案编号:11010802021510 niuxiaotong@pcpop.com 17352615567 <a style="color:#777777;" target="_blank" href="http://www.it168.com/jubao.html"> 未成年人举报专区</a></p> <p>广播电视节目制作经营许可证(京) 字第1234号 中国互联网协会会员</p> <p>违法和不良信息举报电话:18101376593</p> </div> </div> <!--footer部分结束--> <script> var _hmt = _hmt || []; (function() { var hm = document.createElement("script"); hm.src = "https://hm.baidu.com/hm.js?5016281862f595e78ffa42f085ea0f49"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(hm, s); })(); </script> <script> var _hmt = _hmt || []; (function() { var hm = document.createElement("script"); hm.src = "https://hm.baidu.com/hm.js?540bee2c1a1b922df9e0a0b922440b73"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(hm, s); })(); </script> <!-- END STAT PV --> </body> </html>