1.前言

最新在公司做一个项目,需要一些文章类的数据,当时就想到了用网络爬虫去一些技术性的网站爬一些,当然我经常去的就是博客园,于是就有下面的这篇文章。

程序源码:CSDN下载地址

2.准备工作

我需要把我从博客园爬取的数据,保存起来,最好的方式当然是保存到数据库中去了,好了我们先建一个数据库,在来一张表,保存我们的数据,其实都很简单的了啊,如下图所示

网络爬虫+HtmlAgilityPack+windows服务从博客园爬取20万博文

BlogArticleId博文自增ID,BlogTitle博文标题,BlogUrl博文地址,BlogAuthor博文作者,BlogTime博文发布时间,BlogMotto作者座右铭,BlogDepth蜘蛛爬虫爬取的深度,IsDeleted是否删除。

数据库表也创建好了,我们先来一个数据库的帮助类。

    /// <summary>
    /// 数据库帮助类
    /// </summary>
    public class MssqlHelper
    {
        #region 字段属性
        /// <summary>
        /// 数据库连接字符串
        /// </summary>
        private static string conn = "Data Source=.;Initial Catalog=Cnblogs;User ID=sa;Password=123";
        #endregion

        #region DataTable写入数据
        public static void GetData(string title, string url, string author, string time, string motto, string depth, DataTable dt)
        {
            DataRow dr;

            dr = dt.NewRow();
            dr["BlogTitle"] = title;
            dr["BlogUrl"] = url;
            dr["BlogAuthor"] = author;
            dr["BlogTime"] = time;
            dr["BlogMotto"] = motto;
            dr["BlogDepth"] = depth;

            //2.0 将dr追加到dt中
            dt.Rows.Add(dr);
        }
        #endregion

        #region 插入数据到数据库
        /// <summary>
        /// 插入数据到数据库
        /// </summary>
        public static void InsertDb(DataTable dt)
        {
            try
            {
                using (System.Data.SqlClient.SqlBulkCopy copy = new System.Data.SqlClient.SqlBulkCopy(conn))
                {
                    //3.0.1 指定数据插入目标表名称
                    copy.DestinationTableName = "BlogArticle";

                    //3.0.2 告诉SqlBulkCopy对象 内存表中的 OrderNO1和Userid1插入到OrderInfos表中的哪些列中
                    copy.ColumnMappings.Add("BlogTitle", "BlogTitle");
                    copy.ColumnMappings.Add("BlogUrl", "BlogUrl");
                    copy.ColumnMappings.Add("BlogAuthor", "BlogAuthor");
                    copy.ColumnMappings.Add("BlogTime", "BlogTime");
                    copy.ColumnMappings.Add("BlogMotto", "BlogMotto");
                    copy.ColumnMappings.Add("BlogDepth", "BlogDepth");

                    //3.0.3 将内存表dt中的数据一次性批量插入到OrderInfos表中
                    copy.WriteToServer(dt);
                    dt.Rows.Clear();
                }
            }
            catch (Exception)
            {
                dt.Rows.Clear();

            }
        }
        #endregion

    }

3.日志

来个日志,方便我们查看,代码如下。

    /// <summary>
    /// 日志帮助类
    /// </summary>
    public class LogHelper
    {
        #region 写入日志
        //写入日志
        public static void WriteLog(string text)
        {
            //StreamWriter sw = new StreamWriter(AppDomain.CurrentDomain.BaseDirectory + "\\log.txt", true);
            StreamWriter sw = new StreamWriter("F:" + "\\log.txt", true);
            sw.WriteLine(text);
            sw.Close();//写入
        }
        #endregion

    }

4.爬虫

我的网络蜘蛛爬虫,用的一个第三方类库,代码如下。

namespace Feng.SimpleCrawler
{
    using System;

    /// <summary>
    /// The add url event handler.
    /// </summary>
    /// <param name="args">
    /// The args.
    /// </param>
    /// <returns>
    /// The <see cref="bool"/>.
    /// </returns>
    public delegate bool AddUrlEventHandler(AddUrlEventArgs args);

    /// <summary>
    /// The add url event args.
    /// </summary>
    public class AddUrlEventArgs : EventArgs
    {
        #region Public Properties

        /// <summary>
        /// Gets or sets the depth.
        /// </summary>
        public int Depth { get; set; }

        /// <summary>
        /// Gets or sets the title.
        /// </summary>
        public string Title { get; set; }

        /// <summary>
        /// Gets or sets the url.
        /// </summary>
        public string Url { get; set; }

        #endregion
    }
}
AddUrlEventArgs.cs

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-08-05
  • 2022-01-27
  • 2022-03-05
  • 2021-10-09
  • 2022-12-23
  • 2022-02-23
猜你喜欢
  • 2021-10-01
  • 2022-12-23
  • 2021-10-13
  • 2021-05-26
  • 2022-12-23
  • 2021-08-17
  • 2021-05-20
相关资源
相似解决方案