C#获取搜索引擎输入的关键字

  C#获取搜索引擎输入的关键字源代码

using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
//分析来源页面,获得搜索引擎输入的关键字
namespace SearchEngineKeyword
{
    /// <summary>
    /// 搜索引擎实体
    /// </summary>
    internal class Enginer
    {
        private string _EngineName = string.Empty;
        private string _EngineCode = string.Empty;
        private Regex _EngineKeywordRegex;
        /// <summary>
        /// 搜索引擎名称
        /// </summary>
        public string EngineName { get { return _EngineName; }  }
        /// <summary>
        /// 搜索引擎关键字编码
        /// </summary>
        public string EngineCode { get { return _EngineCode; }}
        /// <summary>
        /// 搜索引擎关键字键正则
        /// </summary>
        public Regex EngineKeywordRegex { get { return _EngineKeywordRegex; } }
        /// <summary>
        /// 搜索引擎构造函数
        /// </summary>
        /// <param name="_EngineName">搜索引擎名称</param>
        /// <param name="_EngineCode">搜索引擎关键字编码</param>
        /// <param name="_EngineSearchKeyRegex">搜索引擎关键字键名称正则</param>
        public Enginer(string _EngineName, string _EngineCode, string _EngineSearchKeyRegex)
        {
            this._EngineName = _EngineName.ToLower();
            this._EngineCode = _EngineCode.ToLower();
            this._EngineKeywordRegex = new Regex(_EngineSearchKeyRegex + "=([^&]*)", RegexOptions.IgnoreCase | RegexOptions.Compiled);
        }
    }
    /// <summary>
    /// 搜索引擎关键字分析类
    /// </summary>
    public class Parse
    {
        private string _Keyword = "NaN", _EngineName = "NaN";
        /// <summary>
        /// 来源Uri对象
        /// </summary>
        private Uri uReferrer;
        /// <summary>
        /// 搜索引擎列表
        /// </summary>
        private static List<Enginer> Enginers;
        /// <summary>
        /// 搜索引擎名称,只读
        /// </summary>
        public string EngineName { get { return _EngineName; } }
        /// <summary>
        /// 搜索引擎输入的关键字,只读
        /// </summary>
        public string Keyword { get { return _Keyword; } }
        /// <summary>
        /// 初始化搜索引擎列表
        /// <para>只添加了google搜索,baidu.com,cn.bing.com,www.yahoo.cn,soso.com,sogou.com,youdao.com 7个搜索引擎,其他的自己可以自己添加</para>
        /// </summary>
        private static void InitEnginers()
        {
            Enginers = new List<Enginer>();
            Enginers.Add(new Enginer("google", "utf-8", "(q)"));
            Enginers.Add(new Enginer("bing", "utf-8", "(q)"));
            Enginers.Add(new Enginer("yahoo", "utf-8", "(q)"));
            Enginers.Add(new Enginer("youdao", "utf-8", "(q)"));

            Enginers.Add(new Enginer("baidu", "gb2312", "(wd?|word)"));
            Enginers.Add(new Enginer("sogou", "gb2312", "(query)"));
            Enginers.Add(new Enginer("soso", "gb2312", "(w)"));
        }
        /// <summary>
        /// 添加新搜索引擎对象
        /// </summary>
        /// <param name="EngineName">搜索引擎名称</param>
        /// <param name="EngineCode">搜索引擎关键字编码</param>
        /// <param name="EngineSearchKeyRegex">搜索引擎关键字键名称正则</param>
        public static void AddEnginer(string EngineName, string EngineCode, string EngineSearchKeyRegex)
        {
            if (Enginers == null) InitEnginers();
            foreach (Enginer enginer in Enginers) if (enginer.EngineName == EngineName) return;
            Enginers.Add(new Enginer(EngineName, EngineCode, EngineSearchKeyRegex));
        }
        /// <summary>
        /// 搜索引擎关键字分析类构造函数
        /// </summary>
        /// <param name="uReferrer">来源Uri对象</param>
        public Parse(Uri uReferrer)
        {
            if (Enginers == null) InitEnginers();
            if (uReferrer != null)
            {
                this.uReferrer = uReferrer;
                ParseKeyword();
            }
        }
        /// <summary>
        /// 分析关键字
        /// </summary>
        private void ParseKeyword()
        {
            string host = uReferrer.Host.ToLower()
                , query = uReferrer.Query;//这里注意不能直接uReferrer.ToString(),要不gb2312编码的关键字会得到乱码
            if (string.IsNullOrEmpty(query)) query = uReferrer.ToString();
            foreach (Enginer e in Enginers)
                if (host.IndexOf(e.EngineName) != -1)
                {
                    _EngineName = host;
                    _Keyword = e.EngineKeywordRegex.Match(query).Groups[2].Value;
                    if (_Keyword.Trim() != "") _Keyword = HttpUtility.UrlDecode(_Keyword, Encoding.GetEncoding(e.EngineCode));
                    break;
                }
        }
    }
}

 

加支付宝好友偷能量挖...


原创文章,转载请注明出处:C#获取搜索引擎输入的关键字

评论(0)Web开发网
阅读(267)喜欢(0)Asp.Net/C#/WCF