C#.Net基于正则表达式抓取百度百家文章列表的方法示例-成都创新互联网站建设

关于创新互联

多方位宣传企业产品与服务 突出企业形象

公司简介 公司的服务 荣誉资质 新闻动态 联系我们

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> URL链接:<a href="http://kswsj.cn/article/gjpoog.html">http://kswsj.cn/article/gjpoog.html</a> </div> </div> <div class="other"> <h3>其他资讯</h3> <ul> <li> <a href="/article/jcchgi.html">ASP.NET应用程序生命周期有多长</a> </li><li> <a href="/article/jcchjh.html">VMwareWorkstation12新建虚拟机</a> </li><li> <a href="/article/jcchii.html">@Scheduled注解</a> </li><li> <a href="/article/jcchep.html">php打开json文件乱码的解决方法</a> </li><li> <a href="/article/jcchic.html">ASP.NET4中的URLRouting怎么使用</a> </li> </ul> </div> </div> <div class="line"></div> <!--底部--> <footer id="5"> <div class="foot1 container"> <div class="list"> <div class="item"> <a href="javascript:;"> <span class="ico1"><i class="iconfont"></i><img src="/Public/Home/img/ewm.png" alt=""></span> <strong>关注我们</strong> </a> </div> <div class="item"> <a href="" target="_blank"> <span><i class="iconfont"></i></span> <strong>索要报价</strong> </a> </div> <div class="item"> <a href="" target="_blank"> <span><i class="iconfont"></i></span> <strong>我要咨询</strong> </a> </div> <div class="item"> <a href="" target="_blank"> <span><i class="iconfont"></i></span> <strong>找到我们</strong> </a> </div> <div class="item"> <a href="" target="_blank"> <span><i class="iconfont"></i></span> <strong>投诉建议</strong> </a> </div> </div> <div class="tel"> <dl> <tel><a href="tel:400-028-6601" target="_blank">400-028-6601</a></tel><br> <span>也许您需要专业的服务,欢迎来电咨询</span> </dl> <dl> <tel><a href="tel:18980820575" target="_blank">18980820575</a></tel><br> <span>您的需求,是我们前进的动力</span> </dl> </div> </div> <div class="friend"> <div class="container"> <span class="tit">友情链接:</span> <div class="inner"> <a href="https://www.cdcxhl.com/xiangyingshi.html" target="_blank">成都响应式网站建设</a><a href="https://www.cdcxhl.com/xiaochengx.html" target="_blank">成都微信小程序开发公司</a><a href="https://www.cdcxhl.com/pinpai.html" target="_blank">品牌网站建设</a><a href="https://www.cdcxhl.com/qiye.html" target="_blank">成都企业网站建设</a><a href="https://www.cdcxhl.com/tuoguan/dianxin/" target="_blank">绵阳服务器托管</a><a href="https://www.cdcxhl.com/ruanwen/" target="_blank">软文发稿投放平台</a><a href="https://www.cdcxhl.com/app.html" target="_blank">成都app软件开发公司</a><a href="https://www.cdcxhl.com/jigui/" target="_blank">机柜租用</a><a href="https://www.cdcxhl.com/" target="_blank">成都建站</a><a href="https://www.cdcxhl.com/yingxiao.html" target="_blank">成都营销网站建设</a><a href="https://www.cdcxhl.com/cloud/" target="_blank">成都云服务器</a><a href="https://www.cdcxhl.com/gaofang/" target="_blank">高防主机租用</a><a href="https://www.cdcxhl.com/hangyead/" target="_blank">广告投放平台</a><a href="https://www.cdcxhl.com/xiangyingshi.html" target="_blank">响应式网站建设</a><a href="https://www.cdcxhl.com/app.html" target="_blank">app软件开发公司</a><a href="https://www.cdcxhl.com/quanwang.html" target="_blank">整合营销推广</a><a href="https://www.cdcxhl.com/xiangyingshi.html" target="_blank">成都响应式网站设计</a><a href="https://www.cdcxhl.com/link/" target="_blank">买卖链接</a> </div> </div> </div> <div class="foot"> <div class="container"> <div class="footNav"> <h3>网站建设</h3> <a href="http://www.cdkjz.cn/fangan/xuexiao/" target="_blank">校园网站建设方案</a><a href="http://www.cxjianzhan.com/" target="_blank">网站建设公司</a><a href="http://www.cdkjz.cn/" target="_blank">成都网站建设</a> </div> <div class="footNav"> <h3>服务器托管</h3> <a href="https://www.cdcxhl.com/tuoguan/" target="_blank">IDC机房托管</a><a href="https://www.cdcxhl.com/idc/yldx.html" target="_blank">义龙电信机房</a><a href="https://www.xwcx.net/" target="_blank">成都机柜租用</a> </div> <div class="footNav"> <h3>网站制作</h3> <a href="http://www.cxhlcq.com/zhizuo/" target="_blank">重庆网站制作</a><a href="http://www.cxjianzhan.com/mobile/" target="_blank">wap网站制作</a><a href="http://www.cqcxhl.com/" target="_blank">重庆网站制作</a> </div> <div class="footNav"> <h3>企业服务</h3> <a href="https://www.cdcxhl.com/service/service.html" target="_blank">工商服务</a><a href="https://www.cdcxhl.com/mianfei/jianzhan/" target="_blank">成都免费建站</a><a href="https://www.cdcxhl.com/shoulu/" target="_blank">免费收录</a> </div> <div class="fr ecode"> <div class="fl"> <img src="/Public/Home/img/ewm.jpg"> <p>关注企业微信</p> </div> <div class="fr slogan"> <p class="icon"> <a class="ph" href=""><i class="iconfont"></i></a> <a class="qq" href="tencent://message/?uin=1683211881&Site=&Menu=yes"><i class="iconfont"></i></a> </p> <p> <i>想要找 </i> <a href="">小程序开发</a>、<a href="">APP开发</a>、 <a href="">营销型网站建设</a>、<a href="">网站建设</a>、 <i><a href="">网站定制开发</a></i> ,就选<a href="">创新互联</a> </p> </div> </div> </div> <div class="bottom container"> <p class="fl"> 版权所有:成都创新互联科技有限公司 备案号:<a href="https://beian.miit.gov.cn/" target="_blank" rel="nofollow">蜀ICP备19037934号</a> 服务热线:028-86922220 </p> <p class="fr"> <a href="https://www.cdxwcx.com/" target="_blank">成都网站建设</a>: <a href="https://www.cdcxhl.com/" target="_blank">创新互联</a> </p> </div> </div> </footer> <!--在线咨询--> <div class="fot"> <ul> <li> <a href="https://p.qiao.baidu.com/cps/mobileChat?siteId=11284691&userId=6256368&type=1&reqParam=%20{%22from%22:0,%22sessionid%22:%22%22,%22siteId%22:%2211284691%22,%22tid%22:%22-1%22,%22userId%22:%226256368%22,%22ttype%22:1,%22siteConfig%22:%20{%22eid%22:%226256368%22,%22queuing%22:%22%22,%22siteToken%22:%226ce441ff9e2d6bedbdfc2a4138de449e%22,%22userId%22:%226256368%22,%22isGray%22:%22false%22,%22wsUrl%22:%22wss://p.qiao.baidu.com/cps3/websocket%22,%22likeVersion%22:%22generic%22,%22siteId%22:%2211284691%22,%22online%22:%22true%22,%22webRoot%22:%22//p.qiao.baidu.com/cps3/%22,%22bid%22:%22160142915792139572%22,%22isSmallFlow%22:0,%22isPreonline%22:0,%22invited%22:0%20},%22config%22:%20{%22themeColor%22:%224d74fa%22%20}%20}&appId=&referer=&iswechat=0&expectWaiter=-1&openid=null&otherParam=null&telephone=null&speedLogId=null&eid=null&siteToken=6ce441ff9e2d6bedbdfc2a4138de449e" target="_blank"> <img src="/Public/Home/img/fot1.png" alt=""> <p>在线咨询</p> </a> </li> <li> <a href="tel:18980820575" target="_blank"> <img src="/Public/Home/img/fot2.png" alt=""> <p>拨打电话</p> </a> </li> </ul> </div> </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>