Example #1
0
        private void CrawlerRuleForm_Load(object sender, EventArgs e)
        {
            cbxChapterTitleType.SelectedIndex = 0;
            var bookConfigs = BooksUtils.GetAllBookConfig();

            cbxBookName.DataSource    = bookConfigs;
            cbxBookName.DisplayMember = nameof(Book.BookName);
            cbxBookName.ValueMember   = nameof(Book.Id);

            var rules = CrawlerRuleUtils.GetAllCrawlerRules();

            cbxSelectedRule.DataSource    = rules;
            cbxSelectedRule.DisplayMember = nameof(CrawlerRule.RuleName);

            if (bookConfigs.Count > 0)
            {
                var book = bookConfigs.FirstOrDefault();
                cbxBookName.Text     = book.BookName;
                txtAuthor.Text       = book.Author;
                cbxSelectedRule.Text = book.CrawlerRuleName;
                txtStartUrl.Text     = book.StartUrl;
            }

            cbxRuleName.DataSource    = JsonConvert.DeserializeObject <List <CrawlerRule> >(JsonConvert.SerializeObject(rules));
            cbxRuleName.DisplayMember = nameof(CrawlerRule.RuleName);
        }
Example #2
0
        /// <summary>
        /// 保存爬虫规则
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSaveRules_Click(object sender, EventArgs e)
        {
            var chapterRootPath  = txtChapterRootPath.Text;
            var chapterTitleType = cbxChapterTitleType.Text;
            var chapterTitlePath = txtChapterTitlePath.Text;
            var nextPagePath     = txtNextPagePath.Text;
            var ruleName         = cbxRuleName.Text;
            var awaitTimeText    = txtAwaitTime.Text;
            var awaitTime        = 1;

            if (string.IsNullOrWhiteSpace(chapterRootPath) || string.IsNullOrWhiteSpace(chapterTitlePath) ||
                string.IsNullOrWhiteSpace(nextPagePath) || string.IsNullOrWhiteSpace(ruleName) ||
                string.IsNullOrWhiteSpace(awaitTimeText))
            {
                Console.WriteLine("爬虫规则填写不完整,请输入完整的规则");
                MessageBox.Show("爬虫规则填写不完整,请输入完整的规则");
                return;
            }

            if (int.TryParse(awaitTimeText, out var t))
            {
                if (t >= 0)
                {
                    awaitTime = t;
                }
            }

            CrawlerRuleUtils.SaveCrawlerRule(new CrawlerRule
            {
                RuleName         = ruleName,
                ChapterRootPath  = chapterRootPath,
                ChapterTitlePath = chapterTitlePath,
                ChapterTitleType = chapterTitleType,
                NextPagePath     = nextPagePath,
                AwaitTime        = awaitTime
            });

            var rules = CrawlerRuleUtils.GetAllCrawlerRules();

            var selectedRuleText = cbxSelectedRule.Text;

            cbxSelectedRule.DataSource    = rules;
            cbxSelectedRule.DisplayMember = nameof(CrawlerRule.RuleName);
            if (!string.IsNullOrWhiteSpace(selectedRuleText))
            {
                cbxSelectedRule.Text = selectedRuleText;
            }
        }
Example #3
0
        private void btnEditRule_Click(object sender, EventArgs e)
        {
            var crawlerRuleForm = new CrawlerRuleForm();

            crawlerRuleForm.ShowDialog();

            var bookConfigs = BooksUtils.GetAllBookConfig();

            bookConfigs.ForEach(x =>
            {
                x.BookName = $"{x.BookName}-{x.Author}";
            });
            cbxBookConfig.DataSource    = bookConfigs;
            cbxBookConfig.DisplayMember = nameof(Book.BookName);
            cbxBookConfig.ValueMember   = nameof(Book.Id);
            cbxBookConfig.SelectedIndex = 0;

            this.cbxBookConfig.Tag = CrawlerRuleUtils.GetAllCrawlerRules();
        }
Example #4
0
        private void btnStart_Click(object sender, EventArgs e)
        {
            if (cbxBookConfig.DataSource is List <Book> books)
            {
                var id = cbxBookConfig.SelectedValue as int? ?? 0;
                if (id == 0)
                {
                    Console.WriteLine("请选择有效的书名");
                    return;
                }
                var book = books.FirstOrDefault(x => x.Id == id);
                this.panelBrowser.Tag = book;

                // 读取最新的爬虫规则
                var rules = CrawlerRuleUtils.GetAllCrawlerRules();
                cbxBookConfig.Tag = rules;

                chromeBrowser.FrameLoadEnd += ChromeBrowser_FrameLoadEnd;
                chromeBrowser.Load(book.StartUrl);
            }
        }