高县网站建设设计

将想法与焦点和您一起共享

C#.Net基于正则表达式抓取百度百家文章列表的方法示例

本文实例讲述了C#.Net基于正则表达式抓取百度百家文章列表的方法。分享给大家供大家参考,具体如下:

创新互联长期为近千家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为让胡路企业提供专业的成都网站建设、成都网站制作,让胡路网站改版等技术服务。拥有十年丰富建站经验和众多成功案例,为您定制开发。

工作之余,学习了一下正则表达式,鉴于实践是检验真理的唯一标准,于是便写了一个利用正则表达式抓取百度百家文章的例子,具体过程请看下面源码:

一、获取百度百家网页内容

public List GetUrl()
{
  try
  {
    string url = "http://baijia.baidu.com/";
    WebRequest webRequest = WebRequest.Create(url);
    WebResponse webResponse = webRequest.GetResponse();
    StreamReader reader = new StreamReader(webResponse.GetResponseStream());
    string result = reader.ReadToEnd();
    reader.Close();
    webResponse.Close();
    return AnalysisHtml(result);
  }
  catch (Exception ex)
  {
    throw ex;
  }
}

二、通过正则表达式筛选

public List AnalysisHtml(string htmlContent)
{
  List list = new List();
  string strPattern = "

(?[^<]+)</a></h4>.*\\s*<p\\s*class=\"feeds-item-text\">(?<Abstract>[^<]+)<a\\s*href=\"(?<Url>.*)\"\\s*target=\"_blank\"\\s*class=\"feeds-item-more\"\\s*mon=\".*\\s*\">.*\\s*</a></p>"; Regex regex = new Regex(strPattern, RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.CultureInvariant); if (regex.IsMatch(htmlContent)) { MatchCollection matchCollection = regex.Matches(htmlContent); foreach (Match match in matchCollection) { string[] str = new string[3]; str[0] = match.Groups[1].Value;//获取到的是列表数据的标题 str[1] = match.Groups[2].Value;//获取到的是内容 str[2] = match.Groups[3].Value;//获取到的是链接到的地址 list.Add(str); } } return list; } </pre></div><p><strong>附:</strong>完整实例代码点击此处<strong>本站下载</strong>。</p><p><strong>PS:这里再为大家提供2款非常方便的正则表达式工具供大家参考使用:</strong></p><p><strong>JavaScript正则表达式在线测试工具:<br /></strong>http://tools.jb51.net/regex/javascript</p><p><strong>正则表达式在线生成工具:<br /></strong>http://tools.jb51.net/regex/create_reg</p><p>更多关于C#相关内容感兴趣的读者可查看本站专题:《C#正则表达式用法总结》、《C#编码操作技巧总结》、《C#常见控件用法教程》、《WinForm控件用法总结》、《C#数据结构与算法教程》、《C#面向对象程序设计入门教程》及《C#程序设计之线程使用技巧总结》</p><p>希望本文所述对大家C#程序设计有所帮助。</p> <br> 当前题目:C#.Net基于正则表达式抓取百度百家文章列表的方法示例 <br> 网页地址:<a href="http://scgaoxian.com/article/gjpoog.html">http://scgaoxian.com/article/gjpoog.html</a> </div> </div> <div class="news_r"> <h3>其他资讯</h3> <ul> <li> <a href="/article/deopidg.html">闲置域名怎么 域名如何</a> </li><li> <a href="/article/deopehp.html">java计时器重置代码 用java编写一个计时器程序</a> </li><li> <a href="/article/deopepg.html">阿里云服务器什么品牌 阿里云服务器怎么样?用一年了来说说吧</a> </li><li> <a href="/article/deopioj.html">c语言如何调用开根号函数 c语言怎么调用根号</a> </li><li> <a href="/article/deopejo.html">go语言elseif go语言编程</a> </li> </ul> </div> </div> <!--尾部begin--> <!--尾部begin--> <footer> <div class="f_bg"> <div class="wrap"> <div class="links"> <h2 class="h2">广皓图文建站解决方案<a href="../solution/" title="更多" class="more">更多+</a></h2> <ul> <li><a href="../solution/xiaochengxu.html" title="小程序定制解决方案">小程序定制解决方案</a></li> <li><a href="../solution/qiyewz.html" title="企业网站建设解决方案">企业网站建设解决方案</a></li> <li><a href="../solution/menhuwz.html" title="行业门户网站建设解决方案">行业门户网站建设解决方案</a></li> <li><a href="../solution/yingxiaowz.html" title="营销型网站建设解决方案">营销型网站建设解决方案</a></li> <li><a href="../solution/waimaowz.html" title="外贸网站建设解决方案">外贸网站建设解决方案</a></li> <li><a href="../solution/pingpaiwz.html" title="品牌形象网站建设解决方案">品牌形象网站建设解决方案</a></li> <li><a href="../solution/dianziwz.html" title="数码、电子产品网站建设解决方案">数码、电子产品网站建设解决方案</a></li> <li><a href="../solution/jituanwz.html" title="集团、上市企业网站建设解决方案">集团、上市企业网站建设解决方案</a></li> <li><a href="../solution/dichanwz.html" title="房地产、地产项目网站建设解决方案">房地产、地产项目网站建设解决方案</a></li> <li><a href="../solution/zhubaowz.html" title="珠宝高端奢侈品网站建设解决方案">珠宝高端奢侈品网站建设解决方案</a></li> </ul> </div> <div class="links w2"> <h2 class="h2">我们的实力<a href="../about/" title="更多" class="more">更多+</a></h2> <ul> <li>10年专业互联网服务经验</li> <li>高县高端建站设计团队</li> <li>资深行业分析策划</li> <li>B2C营销型网站建设者</li> <li>前沿视觉设计、研发能力</li> <li>前端代码深度符合SEO优化</li> <li>高县市高新技术企业证书</li> <li>具有完备的项目管理</li> <li>完善的售后服务体系</li> <li>深厚的网络运营经验</li> <li>时刻新技术研发能力</li> <li>16个网站系统软件著作权</li> </ul> </div> <div class="f_div2_r"> <h2 class="h2">关于广皓图文网站建设<a href="../about/" title="更多" class="more">更多+</a></h2> 广皓图文网站设计,为客户量身定制各类网站建设业务,包括企业型、电子商务型、行业门户型、品牌建立型等各类网站,实战经验丰富,成功案例众多。以客户利益为出发点,广皓图文网站建设网站制作为客户规划、定制符合企业需求、带有营销价值的建站方案,提供从网站前期定位分析策划到网站界面设计... </div> <div class="c_l"></div> </div> <div class="wrap"> <div class="link"> 友情链接: <a href="http://chengdu.cdcxhl.com/xcx/" title="成都小程序" target="_blank">成都小程序</a>   <a href="http://www.028jzp.com/" title="九针服饰" target="_blank">九针服饰</a>   <a href="http://chengdu.cdweb.net/weixinkaifa/qiyeweixin.html" title="企业微信定制" target="_blank">企业微信定制</a>   <a href="http://www.xywzsj.com/" title="钢筋机械设备" target="_blank">钢筋机械设备</a>   <a href="http://www.whxishu.com/" title="whxishu.com" target="_blank">whxishu.com</a>   <a href="http://www.csjierui.cn/" title="csjierui.cn" target="_blank">csjierui.cn</a>   <a href="http://www.scjbc.cn/" title="小谭建站" target="_blank">小谭建站</a>   <a href="http://www.muyideyu.com/" title="营山月子护理" target="_blank">营山月子护理</a>   <a href="http://www.dmvi.cn/" title="成都广告设计公司" target="_blank">成都广告设计公司</a>   <a href="http://www.gtmaxf.com/" title="四川国泰民安消防" target="_blank">四川国泰民安消防</a>    </div> </div> <div class="wrap"> <div class="f_div3"> <span class="l">高县网站制作案例©2025 青羊区广皓图文设计工作室(个体工商户) 高县建站广皓图文 版权所有 | <a href="http://www.scgaoxian.com" target="_blank">高县网站设计</a><a href="http://www.scgaoxian.com" target="_blank">www.scgaoxian.com</a></span> <span class="r"><a href="https://beian.miit.gov.cn/" target="_blank" rel="nofollow">蜀ICP备2025118593号-9</a></span> </div> </div> </div> </footer> <!--尾部end--> <script language="javascript" src="/Public/Home/js/foot.js"></script> <!--尾部end--> <!--侧边栏begin--> <div class="side"> <ul> <li id="qqonline_xbceo"><a href="tencent://message/?uin=631063699&Site=&Menu=yes"><i class="bgs1"></i>QQ咨询</a></li> <li class="shangqiao"><a href="tencent://message/?uin=532337155&Site=&Menu=yes" title="在线咨询"> <div><i class="bgs2"></i>在线咨询</div> </a></li> <li class="sideewm"><i class="bgs3"></i>官方微信 <div class="ewBox"></div> </li> <li class="sideetel"><i class="bgs4"></i>联系电话 <div class="telBox"> <dd class="bgs1"><span>座机</span><a href="tel:028-86922220" target="_blank">028-86922220</a></dd> <dd class="bgs2"><span>手机</span><a href="tel:13518219792" target="_blank">13518219792</a></dd> </div> </li> <li class="sidetop" onClick="goTop()" id="sidetop"><i class="bgs6"></i>返回顶部</li> </ul> </div> <script type="text/javascript"> $('.sideewm').hover(function(){ $('.ewBox').stop().fadeIn(); },function(){ $('.ewBox').stop().fadeOut(); }); $('.sideetel').hover(function(){ $('.telBox').stop().fadeIn(); },function(){ $('.telBox').stop().fadeOut(); }); </script> <!-- WPA start --> <!-- WPA end --> <!--侧边栏end--> </body> </html> <script> $(".con img").each(function(){ var src = $(this).attr("src"); //获取图片地址 var str=new RegExp("http"); var result=str.test(src); if(result==false){ var url = "https://www.cdcxhl.com"+src; //绝对路径 $(this).attr("src",url); } }); window.onload=function(){ document.oncontextmenu=function(){ return false; } } </script>