C# 采集2-采集,一般过滤

采集网页,我是从网上搜的,确实很多人在这方面已经做了很多,感谢他们的共享资源,让我们工作变得更加快。

 

采集网页源码:这个暂时还没意外就不改了,

 

         /// <summary> 
        /// 取得网页源码 
        /// 对于带BOM的网页很有效,不管是什么编码都能正确识别 
        /// </summary> 
        /// <param name="url">网页地址, </param>  
        /// <returns>返回网页源文件</returns> 
        public static string GetHtmlSource(string url)
        {
            WebClient myWebClient = new WebClient();
            byte[] myDataBuffer = myWebClient.DownloadData(url);
            return Encoding.Default.GetString(myDataBuffer);
        }
 
 /// <summary>
        /// 简单过滤,定位到信息区域有用
        /// </summary>
        /// <param name="code">网页源码</param>
        /// <param name="wordsBegin">开始</param>
        /// <param name="wordsEnd">结束</param>
        /// <returns>主题内容</returns>
        public static string SniffwebCode(string code, string wordsBegin, string wordsEnd)
        {
            string NewsTitle = "";
            Regex regex1 = new Regex("" + wordsBegin + @"(?<title>[\s\S]+?)" + wordsEnd + "", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            for (Match match1 = regex1.Match(code); match1.Success; match1 = match1.NextMatch())
            {
                NewsTitle = match1.Groups["title"].ToString();
            }
            return NewsTitle;
        }
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值