简单的网页采集程序

最近开发一个项目,甲方提出在另一个网站中采集信息列表并跳转,于是就简单做了一个信息列表采集功能

       /// <summary>
        /// 采集视频列表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public static void getVideos(object sender, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                string strHtml=GetWholeHtmlCode("<span style="font-family: Arial, Helvetica, sans-serif;">http://www.xxx.com/</span>");
<span style="white-space:pre">		</span>//包含列表的内容截取
                strHtml = strHtml.Substring(strHtml.IndexOf("<div class='x#xxd'>"));
                strHtml = strHtml.Substring(0, strHtml.IndexOf("<div id=Footer>"));

                Regex reg = new Regex(@"(?is)<a[^>]+?href=(['""]?)(?<url>[^'""\s>]+).+?title=(['""]?)(?<title>[^'""\s>]+)\1[^>]*>(?<text>(?:(?!</?a\b).)*)</a>");
                string str = "";
                MatchCollection mc = reg.Matches(strHtml);
                foreach (Match m in mc)
                {
                    try
                    {
                        string url = m.Groups["url"].Value;
                        string title = m.Groups["title"].Value;
                        if (!url.Contains("http:/"))
                        {
                            url = "http://www.xxx.com/" + m.Groups["url"].Value;
                        }
                        // 如果url已经添加了,则中断循环
                        string sql = " SELECT [Path]  FROM [ArticInfo] where [ArticPath]='"+url+"'";
                        object obj = SqlHelper.ExecuteScalar(Art_conn, CommandType.Text, sql);
                        if (obj != null)
                        {
                            continue;
                        }
<span style="white-space:pre">			</span>//添加到表中
                        AddHtmlToArt(url, title);
                    }
                    catch
                    {
                        continue;
                    }

                }

            }
            catch
            {

            }
        }


<pre name="code" class="csharp">/// <summary>
        /// 根据url获取html
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

 
 public static string GetWholeHtmlCode(string url)
        {
            string strHtml = string.Empty;
            StreamReader strReader = null;
            HttpWebResponse wrpContent = null;
            try
            {
                HttpWebRequest wrqContent = (HttpWebRequest)WebRequest.Create(url);
                wrqContent.Timeout = 300000;
                wrpContent = (HttpWebResponse)wrqContent.GetResponse();
                if (wrpContent.StatusCode != HttpStatusCode.OK)
                {

                    strHtml = "Sorry, the web page is not run successful";
                }
                if (wrpContent != null)
                {
                    strReader = new StreamReader(wrpContent.GetResponseStream(), Encoding.UTF8);
                    strHtml = strReader.ReadToEnd();
                }
            }
            catch (Exception e)
            {
                strHtml = e.Message;
            }
            finally
            {
                if (strReader != null)
                    strReader.Close();
                if (wrpContent != null)
                    wrpContent.Close();
            }
            return strHtml.Replace("\r", "").Replace("\n", "");
        }



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值