C#爬虫程序

public string doGet(string url)
        {
            string result = null;
            try
            {
                WebRequest req = WebRequest.Create(url);
                HttpWebRequest httpreg = (HttpWebRequest)req;
                httpreg.Method = "GET";
                httpreg.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; CIBA)";
                WebResponse resp = httpreg.GetResponse();
                StreamReader reader = new StreamReader(resp.GetResponseStream(), Encoding.GetEncoding("utf-8"));
                result = reader.ReadToEnd();
            }
            catch (Exception ex)
            {
                return "";
            }
            return result;
        }

        /// <summary> 
        /// 返回匹配多个的集合值 
        /// </summary> 
        /// <param name="start">开始html tag</param> 
        /// <param name="end">结束html tag</param> 
        /// <param name="html">html</param> 
        /// 

        public static IList<string> GetHtmls(string start, string end, string html)
        {
            IList<string> list = new List<string>();
            try
            {
                string pattern = string.Format("{0}(?<g>(.|[/r/n])+?){1}", start, end);//匹配URL的模式,并分组 
                MatchCollection mc = Regex.Matches(html, pattern);//满足pattern的匹配集合 
                if (mc.Count != 0)
                {
                    foreach (Match match in mc)
                    {
                        GroupCollection gc = match.Groups;
                        list.Add(gc["g"].Value);
                    }
                }
            }
            catch { }
            return list;
        }

        public static string GetHtml(string start, string end, string html)
        {
            string ret = string.Empty;
            try
            {
                string pattern = string.Format("{0}(?<g>(.|[/r/n])+?)?{1}", start, end);//匹配URL的模式,并分组 
                ret = Regex.Match(html, pattern).Groups["g"].Value;

            }
            catch { }

            return ret;
        }

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值