基本信息
源码名称:C# 关键字挖掘机源码下载(亲测可用)
源码大小:0.47M
文件格式:.rar
开发语言:C#
更新时间:2016-11-10
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559

本次赞助数额为: 5 元 
   源码介绍

关键字挖掘机源码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Windows.Forms;

namespace KeyWordsExc
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        Thread t1;
        private void baidu_Click(object sender, EventArgs e)
        {
            Config.WriteIniData("KeyWord", "key", txtInputKey.Text.Trim(), Application.StartupPath   @"\config\config.ini");
            Config.WriteIniData("Rule", "rule", txtFilter.Text.Trim(), Application.StartupPath   @"\config\config.ini");

            Config.WriteIniData("MaxPage", "maxpage", txtCount.Text.Trim(), Application.StartupPath   @"\config\config.ini");
            Config.WriteIniData("MaxChar", "maxchar", txtMaxKey.Text.Trim(), Application.StartupPath   @"\config\config.ini");
             dataGridView1.Rows.Clear();
            t1 = new Thread(new ThreadStart(Thread1));
            t1.Priority = ThreadPriority.BelowNormal;
            t1.Start();
           
        }
        private void stop_Click(object sender, EventArgs e)
        {
            t1.Abort();
        }
        public  void Thread1()
        {
            int itotal = 0;
            int num = int.Parse(txtCount.Text);//搜索条数
            string sfilter = txtFilter.Text;
            string[]  sfilters=null;
            if (sfilter.Trim() != "")
            { 
                sfilters=sfilter.Trim().Split(',');
            }
            string[] arrKey = txtInputKey.Text.Trim().TrimEnd(',').Split(',');
            for (int iCountKey = 0; iCountKey < arrKey.Length; iCountKey  )
            {
                for (int ipage = 0; ipage < num; ipage  )
                {
                    string url = "http://www.baidu.com/s?wd="   arrKey[iCountKey].Trim()   "&rn="   num   "&pn="   ipage * 9;
                    string html = search(url, "utf-8");
                    BaiduSearch baidu = new BaiduSearch();
                    if (!string.IsNullOrEmpty(html))
                    {
                        int count = baidu.GetSearchCount(html);//搜索条数
                        if (count > 0)
                        {
                            List<Keyword> keywords = baidu.GetKeywords(html, arrKey[iCountKey].Trim());
                            //dataGridView1.DataSource = keywords;
                            for (int icout = 0; icout < keywords.Count; icout  )
                            {
                                try
                                {
                                    itotal  ;
                                    this.Invoke((EventHandler)delegate
                                    {
                                        ltotal.Text = itotal.ToString();
                                        DataGridViewRow drs = new DataGridViewRow();
                                        DataGridViewTextBoxCell txt_id = new DataGridViewTextBoxCell();
                                        DataGridViewTextBoxCell txt_word = new DataGridViewTextBoxCell();
                                        txt_id.Value = itotal;// keywords[icout].ID;
                                        txt_word.Value = keywords[icout].KeyWord;
                                        drs.Cells.Add(txt_id);
                                        drs.Cells.Add(txt_word);
                                        bool isAdd = true;
                                        if (keywords[icout].KeyWord.Length > int.Parse(txtMaxKey.Text))
                                            isAdd = false;
                                        for (int ifi = 0; ifi < sfilters.Length; ifi  )
                                        {
                                            if (keywords[icout].KeyWord.IndexOf(sfilters[ifi].ToString()) >= 0)
                                            {
                                                isAdd = false;
                                            }
                                        }
                                        if (dataGridView1.Rows.Count > 1 && isAdd)
                                            foreach (DataGridViewRow dgvr in dataGridView1.Rows)
                                            {
                                                if (dgvr.Cells[0].Value != null)
                                                {
                                                    if (dgvr.Cells["KeyWord"].Value.ToString().Trim() == keywords[icout].KeyWord.Trim() || keywords[icout].KeyWord.Length > int.Parse(txtMaxKey.Text))
                                                        isAdd = false;
                                                }
                                            }
                                        if (isAdd)
                                            dataGridView1.Rows.Add(drs);
                                    });
                                }
                                catch { }
                            }

                        }
                    }
                }
            }
            MessageBox.Show("生成完成!");
        }
 
       private void google_Click(object sender, EventArgs e)
       {
           int num = 100;
           string url = "http://www.google.com.hk/search?hl=zh-CN&source=hp&q="   txtInputKey.Text.Trim()   "&aq=f&aqi=&aql=&oq=&num="   num   "";
           string html = search(url, "utf-8");
           if (!string.IsNullOrEmpty(html))
           {
               googleSearch google = new googleSearch();
               List<Keyword> keywords = google.GetKeywords(html, txtInputKey.Text.Trim());
               //dataGridView1.Columns[0]..Width = 600;
               dataGridView1.DataSource = keywords;
               //dataGridView1.AutoSizeColumnsMode
           }
       }
       /// <summary>
       /// 搜索处理
       /// </summary>
       /// <param name="url">搜索网址</param>
       /// <param name="Chareset">编码</param>
       public string search(string url, string Chareset)
       {
           HttpState result = new HttpState();
           Uri uri = new Uri(url);
           HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);
           myHttpWebRequest.UseDefaultCredentials = true;
           myHttpWebRequest.ContentType = "text/html";
           myHttpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.0; .NET CLR 1.1.4322; .NET CLR 2.0.50215;)";
           myHttpWebRequest.Method = "GET";
           myHttpWebRequest.CookieContainer = new CookieContainer();
           try
           {
               HttpWebResponse response = (HttpWebResponse)myHttpWebRequest.GetResponse();
               // 从 ResponseStream 中读取HTML源码并格式化 add by cqp
               result.Html = readResponseStream(response, Chareset);
               result.CookieContainer = myHttpWebRequest.CookieContainer;
               return result.Html;
           }
           catch (Exception ex)
           {
               return ex.ToString();
           }
       }
       public string readResponseStream(HttpWebResponse response, string Chareset)
       {
           string result = "";
           using (StreamReader responseReader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(Chareset)))
           {
               result = formatHTML(responseReader.ReadToEnd());
           }
           return result;
       }
       /// <summary>
       /// 描述:格式化网页源码
       /// 
       /// </summary>
       /// <param name="htmlContent"></param>
       /// <returns></returns>
       public string formatHTML(string htmlContent)
       {
           string result = "";
           result = htmlContent.Replace("&raquo;", "").Replace("&nbsp;", "")
                   .Replace("&copy;", "").Replace("/r", "").Replace("/t", "")
                   .Replace("/n", "").Replace("&amp;", "&");
           return result;
       }

        public  class BaiduSearch
    {
        protected string uri = "http://www.baidu.com/s?wd=";
        protected Encoding queryEncoding = Encoding.GetEncoding("gb2312");
        protected Encoding pageEncoding = Encoding.GetEncoding("gb2312");
        protected string resultPattern = @"(?<=找到相关结果[约]?)[0-9,]*?(?=个)";
        public int GetSearchCount(string html)
        {
            int result = 0;
           string searchcount = string.Empty;
 
           Regex regex = new Regex(resultPattern);
           Match match = regex.Match(html);
 
           if (match.Success)
           {
               searchcount = match.Value;
           }
           else
           {
               searchcount = "0";
           }

           if (searchcount.IndexOf(",") > 0)
           {
               searchcount = searchcount.Replace(",", string.Empty);
           }

           int.TryParse(searchcount, out result);

           return result;
       }
 
       public List<Keyword> GetKeywords(string html, string word)
       {
           int i = 1;
           List<Keyword> keywords = new List<Keyword>();
           //string ss="<h3 class=\"t\"><a.*?href=\"(?<url>.*?)\".*?>(?<content>.*?)</a>";
           string ss = ">(?<content>.*?)</a></h3>";
           MatchCollection mcTable = Regex.Matches(html,ss);
           foreach (Match mTable in mcTable)
           {
               if (mTable.Success)
               {
                   Keyword keyword = new Keyword();
                   keyword.ID = i  ;
                   keyword.KeyWord = Regex.Replace(mTable.Groups["content"].Value, "<[^>]*>", string.Empty);
                   //keyword.Link = mTable.Groups["url"].Value;
                   keywords.Add(keyword);
 
               }
           }
 
           return keywords;
       }
     }

        public class googleSearch
        {

            public List<Keyword> GetKeywords(string html, string word)
            {
                int i = 1;
                List<Keyword> keywords = new List<Keyword>();

                Regex regTable = new Regex("<h3 class=\"r\"><a.*?href=\"(?<url>.*?)\".*?>(?<content>.*?)</a>", RegexOptions.IgnoreCase);
                Regex regA = new Regex(@"(?is)<a/b[^>]*?href=(['""]?)(?<link>[^'""/s>] )/1[^>]*>(?<title>.*?)</a>", RegexOptions.IgnoreCase);

                MatchCollection mcTable = regTable.Matches(html);
                foreach (Match mTable in mcTable)
                {
                    if (mTable.Success)
                    {
                        Keyword keyword = new Keyword();
                        keyword.ID = i  ;
                        keyword.KeyWord = Regex.Replace(mTable.Groups["content"].Value, "<[^>]*>", string.Empty);
                        //keyword.Link = mTable.Groups["url"].Value;
                        keywords.Add(keyword);
                    }
                }

                return keywords;
            }
        }

        private void btnOutPut_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFile1 = new SaveFileDialog();
            saveFile1.Filter = "文本文件(.txt)|*.txt";
            saveFile1.FilterIndex = 1;
            if (saveFile1.ShowDialog() == System.Windows.Forms.DialogResult.OK && saveFile1.FileName.Length > 0)
            {
                System.IO.StreamWriter sw = new System.IO.StreamWriter(saveFile1.FileName, false);
                try
                {
                    string sword = "";
                        foreach(DataGridViewRow dr in dataGridView1.Rows)
                        {
                            sword = dr.Cells["KeyWord"].Value.ToString();
                            sw.WriteLine(sword);
                        }
                    
                }
                catch
                {
                    //throw;
                }
                finally
                {
                    sw.Close();
                }
            }

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.skinEngine1.SkinFile = "config\\MP10.ssk";
            string sKeyword = Config.ReadIniData("KeyWord", "key", "", Application.StartupPath   @"\config\config.ini");
            txtInputKey.Text = sKeyword;
            string sRule = Config.ReadIniData("Rule", "rule", "", Application.StartupPath   @"\config\config.ini");
            txtFilter.Text = sRule;

            string sCount = Config.ReadIniData("MaxPage", "maxpage", "", Application.StartupPath   @"\config\config.ini");
            txtCount.Text=sCount;
           string sMaxchar =  Config.ReadIniData("MaxChar", "maxchar","", Application.StartupPath   @"\config\config.ini");
            txtMaxKey.Text=sMaxchar;
        }

    }
}