C # analyze the search engine url to get the search keyword

Source: Internet
Author: User

The following uses the C # language as an example:

Using System;
Using System. Collections. Generic;
Using System. Text;
Using System. Text. RegularExpressions;

Namespace Util
{
Public class SearchKeyword
{
Public SearchKeyword (){}

// Search Engine Features
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 "},
New string [] {"youdao", "utf8", "q "},
New string [] {"bing", "utf8", "q "},
New string [] {"118114", "gb2312", "kw "}
};
// Search engine name
Private string _ EngineName = "";
Public string EngineName
{
Get
{
Return _ EngineName;
}
}
// Search engine Encoding
Private string _ Coding = "utf8 ″;
Public string Coding
{
Get
{
Return _ Coding;
}
}
// Search engine keyword query parameter name
Private string _ RegexWord = "";
Public string RegexWord
{
Get
{
Return _ RegexWord;
}
}

Private string _ Regex = @ "(";

// Search engine keyword
// Create a regular expression for the Search Keyword
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;
}
}
}
// Obtain the search engine keyword
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;
// + With spaces
MyString = myString. Replace ("+ ","");
If (_ Coding = "gb2312 ″)
{
MyString = GetUTF8String (myString );
}
Else
{
MyString = Uri. UnescapeDataString (myString );
}
}
Return myString;
}
// Complete Transcoding
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;
}
// Convert the single word GB2312 to UTF8 URL Encoding
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 );
}

// Judge whether it is a search engine crawler and return its type
Public string isCrawler (string SystemInfo)
{
String [] BotList = new string [] {"Google", "Baidu", "yisou", "MSN", "Yahoo", "live ",

"Tom", "163", "TMCrawler", "iask", "Sogou", "soso", "youdao", "zhongsou", "3721", "openfind ", "alltheweb", "lycos", "bing", "118114 ″};
Foreach (string Bot in BotList)
{
If (SystemInfo. ToLower (). Contains (Bot. ToLower ()))
{
Return Bot;
}
}
Return "null ";
}

Public bool IsSearchEnginesGet (string str)
{
String [] strArray = new string [] {"Google", "Baidu", "yisou", "MSN", "Yahoo", "live", "tom"

, "163", "TMCrawler", "iask", "Sogou", "soso", "youdao", "zhongsou", "3721", "openfind ", "alltheweb", "lycos", "bing", "118114 ″};
Str = str. ToLower ();
For (int I = 0; I <strArray. Length; I ++)
{
If (str. IndexOf (strArray [I]. ToLower ()> = 0)
{
Return true;
}
}
Return false;
}
}
} Call method: // obtain the source url string url = HttpContext. current. request. urlReferrer. toString (); Pkjg. util. searchKeyword searchkey = new Pkjg. util. searchKeyword (); // determines whether the search engine link if (searchkey. isSearchEnginesGet (url) {// obtain the search Keyword string Keyword = Searchkey. SearchKey (url );// Obtain the search Engine name string Engine = Searchkey. EngineName;}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.