예제 #1
0
        /// <summary>
        /// 当用户点击爬取按钮后就会调用该方法,对相应网址进行资源爬取
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CrawButton_Click(object sender, EventArgs e)
        {
            //每一次新爬取时清空上一次爬取记录的所有图片的index,避免下一次爬取保存index时出现错误
            pictureIndex.Clear();
            resourceBindingSource.Clear();
            saveResourceBindingSource.Clear();
            if (pictureBox.Count > 0)
            {
                for (int j = 0; j < pictureBox.Count; j++)
                {
                    pictureBox[j].Dispose();
                }
            }
            pictureBox.Clear();
            if (checkBoxes.Count > 0)
            {
                for (int j = 0; j < checkBoxes.Count; j++)
                {
                    checkBoxes[j].Dispose();
                }
            }
            checkBoxes.Clear();
            //每一次新爬取时都要把以前爬取得到的图片列表给清空
            CrawlerProject.ImgResourcesContainer.RowImages.Clear();
            CrawlerProject.ImgInputData.Url = this.urlTextBox.Text;
            //此处填入其他的输入
            //bool crawlResult = CrawlerService.StartCrawl(CrawlerProject/*,crawlerService*/);
            //此处填入其他的输入
            Crawl crawl = new Crawl(CrawlerProject);

            CrawlThread = new Thread(crawl.CrawlFun);
            CrawlThread.Start();
            //bool crawlResult = CrawlerService.StartCrawl(CrawlerProject/*,crawlerService*/);
            bool crawlResult = true;


            //if (!crawlResult)            //爬取失败
            //{
            //    //中间还应加上爬取失败的网址,这个网址要得到
            //    messageLabel.Text = this.urlTextBox.Text + "网页爬取失败";
            //}
            //else                //爬取成功
            //{
            //    //中间还应加上成功爬取的网址,这个网址要得到
            //    messageLabel.Text = this.urlTextBox.Text + "网页爬取成功";

            //}
        }
예제 #2
0
        /// <summary>
        /// 实现以图搜图按钮的相关功能
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();  //显示选择文件对话框

            openFileDialog1.InitialDirectory = "c:\\";
            openFileDialog1.Filter           = "txt files (*.txt)|*.txt|All files (*.*)|*.*"; //所有的文件格式
            openFileDialog1.FilterIndex      = 2;
            openFileDialog1.RestoreDirectory = true;
            string path;

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                //获取用户选择的文件路径
                path = openFileDialog1.FileName;
                List <string> imgKeyWord = CrawlerService.ImgSearchImg(path);
                //声明一个url列表,存储得到的url
                List <string> url = new List <string>();
                for (int i = 0; i < imgKeyWord.Count; i++)
                {
                    url.Add(CrawlerService.SearchKeyword(imgKeyWord[i]));
                }
                //只爬取第一个url,因为此url关联最大
                if (url.Count > 0)
                {
                    CrawlerProject.ImgInputData.Url = url[0];
                }
                else
                {
                    return;
                }

                //每一次新爬取时清空上一次爬取记录的所有图片的index,避免下一次爬取保存index时出现错误
                pictureIndex.Clear();
                resourceBindingSource.Clear();
                saveResourceBindingSource.Clear();
                if (pictureBox.Count > 0)
                {
                    for (int j = 0; j < pictureBox.Count; j++)
                    {
                        pictureBox[j].Dispose();
                    }
                }
                pictureBox.Clear();
                if (checkBoxes.Count > 0)
                {
                    for (int j = 0; j < checkBoxes.Count; j++)
                    {
                        checkBoxes[j].Dispose();
                    }
                }
                checkBoxes.Clear();

                //每一次新爬取时都要把以前爬取得到的图片列表给清空
                CrawlerProject.ImgResourcesContainer.RowImages.Clear();

                Crawl  crawl  = new Crawl(CrawlerProject);
                Thread thread = new Thread(crawl.CrawlFun);
                thread.Start();
                //bool crawlResult = CrawlerService.StartCrawl(CrawlerProject/*,crawlerService*/);
                //bool crawlResult = true;


                //if (!crawlResult)            //爬取失败
                //{

                //}
                //else                //爬取成功
                //{


                //}
            }
        }