C#(CSharp) | VC/C++ | ASP(ASP.NET) | SQL Server | OpenGL | CMM | 网站开发SEO | 数控技术 | 地理信息系统 | WINDOWS操作系统 |
联高软件 > 技术文档 > C# > C#获取搜索引擎关键字 C#软件开发参考文档

C#获取搜索引擎关键字

发表:联高软件www.legalsoft.com.cn,本文被阅读:1232

摘要:文章:C#获取搜索引擎关键字 摘要:C#获取搜索引擎关键字usingSystem;usingSystem.Collections;usingSystem.C,发表于北京联高软件有限公司技术文章栏目,代码以高亮显示。
关键字:搜索, 引擎, 获取, string, new, mystring, system, gb, using, utf, public, return, web, mybyte, myword, regex, enginers, enginename


C#获取搜索引擎关键字

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using System.Text;
using System.Text.RegularExpressions;

public partial class Default2 : System.Web.UI.Page
{

private void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
Response.Write(SearchKey("="));
}

private string[][] _Enginers = new string[][] {
new string[]{"google","utf8","q"},
new string[]{"baidu","gb2312","wd"},
new string[]{"yahoo","utf8","p"},
new string[]{"yisou","utf8","search"},
new string[]{"live","utf8","q"},
new string[]{"tom","gb2312","word"},
new string[]{"163","gb2312","q"},
new string[]{"iask","gb2312","k"},
new string[]{"soso","gb2312","w"},
new string[]{"sogou","gb2312","query"},
new string[]{"zhongsou","gb2312","w"},
new string[]{"3721","gb2312","p"},
new string[]{"openfind","utf8","q"},
new string[]{"alltheweb","utf8","q"},
new string[]{"lycos","utf8","query"},
new string[]{"onseek","utf8","q"}
};

//搜索引擎名称
private string _EngineName = "";
public string EngineName
{
get { return _EngineName; }
}
private string _Coding = "utf8";
public string Coding
{
get { return _Coding; }
}
private string _RegexWord = "";
public string RegexWord
{
get { return _RegexWord; }
}

private string _Regex = @"(";

//搜索引擎关键字
public void EngineRegEx(string myString)
{
for (int i = 0, j = _Enginers.Length; i < j; i++)
if (myString.Contains(_Enginers[i][0]))
_EngineName = _Enginers[i][0];
_Coding = _Enginers[i][1];
_RegexWord = _Enginers[i][2];
_Regex += _EngineName + @".+.*[?/&]" + _RegexWord + @"[=:])(?<key>[^&]*)";
break;
}

//得到搜索引擎关键字
public string SearchKey(string myString)
{
EngineRegEx(myString.ToLower());
if (_EngineName != "")
{
Regex myReg = new Regex(_Regex, RegexOptions.IgnoreCase);
Match matche = myReg.Match(myString);
myString = matche.Groups["key"].Value;
myString = myString.Replace("+", " ");
if (_Coding == "gb2312")
myString = GetUTF8String(myString);
else
myString = Uri.UnescapeDataString(myString);
}
return myString;
}

//整句转码
public string GetUTF8String(string myString)
{
Regex myReg = new Regex("(?<key>%..%..)", RegexOptions.IgnoreCase);
MatchCollection matches = myReg.Matches(myString);
string myWord;
for (int i = 0, j = matches.Count; i < j; i++)
{
myWord = matches[i].Groups["key"].Value.ToString();
myString = myString.Replace(myWord, GB2312ToUTF8(myWord));
return myString;
}
}

//单字GB2312转UTF8 URL编码

public string GB2312ToUTF8(string myString)
{
string[] myWord = myString.Split("%");
byte[] myByte = new byte[]
{
Convert.ToByte(myWord[1], 16), Convert.ToByte(myWord[2], 16)
};
Encoding GB = Encoding.GetEncoding("GB2312");
Encoding U8 = Encoding.UTF8;
myByte = Encoding.Convert(GB, U8, myByte);
char[] Chars = new char[U8.GetCharCount(myByte, 0, myByte.Length)];
U8.GetChars(myByte, 0, myByte.Length, Chars, 0);
return new string(Chars);
}

//判断否为搜索引擎爬虫,并返回其类型
public string isCrawler(string SystemInfo)
{
string[] BotList = new string[] { "Google", "Baidu", "MSN", "Yahoo", "TMCrawler", "iask", "Sogou" };
foreach (string Bot in BotList)
if (SystemInfo.ToLower().Contains(Bot.ToLower()))
return Bot;

return "null";
}
}

[C#] C#通过Url抓取网页内容 (5570)
[C#] c#高性能在WEB端产生验证图片 (2661)
[C#] C#的四个基本技巧 (2274)
[C#] 递归枚举排列、组合的C#源码 (6762)
[C#] 通过C#实现集合类纵览.NET Collections及相关技术 (2075)
[C#] 用C#实现Des加密和解密 (3631)
[C#] 数据结构与算法(C#实现)系列---N叉树 (4363)
[C#] 使用设计模式构建通用数据库访问类 (3379)
[C#] C++程序员快速学习C# (2690)
[C#] Convert Code To HTML (C#) (2417)
[C#] 在 ASP.NET 中使用计时器(Timer) (4744)
[C#] C#,深入浅出全接触(五) (4252)
[C#] C#中一些字符串操作的常用用法 (4126)
[C#] C#解决读写包含汉字的txt文件时乱码的问题 (1706)
[C#] 360度全方位比较C#和VB (4390)
[C#] 怎样用C#实现完整文档打印功能 (6455)
[C#] ASP.NET编程中的十大技巧 (1992)
[C#] C#编程入门三部曲:第三步 增加响应用户事件代码 (2267)
[C#] C#开发WAP之GOOGLEADSENSE移动广告代码获取源代码[原创] (3476)

www.315soft.com