There are some code on the Internet, most of which are identified and decoded by the entered keywords. However, the keywords on the Referer source address obtained by the search engine are encoded by urlencode, And the keywords urlencode of each website are different, such as GBK, UTF-8, and gb2312.
Therefore, you must enter a search keyword in Google by using the encoded keyword, for example, "Resolution keyword encoding, encoded "% E8 % A7 % A3 % E6 % 9e % 90% E5 % 85% B3 % E9 % 94% AE % E5 % ad % 97% E7 % BC % 96% E7 % A0 "% 81"
The link address is http://www.google.cn/search? Q = % E8 % A7 % A3 % E6 % 9e % 90% E5 % 85% B3 % E9 % 94% AE % E5 % ad % 97% E7 % BC % 96% E7 % A0 % 81
1. parse the keyword from the above address.
2. Get the encoding name (such as GBK and UTF-8) through the encoded keywords)
3. Use urldecode (keywords, encodecode) to decode the corresponding keywords.
The following is the implementation of Java code:
- Package test;
- Import java. Io. unsupportedencodingexception;
- Import java.net. urldecoder;
- Import java. util. RegEx. matcher;
- Import java. util. RegEx. pattern;
- Public class parseurlkeyword {
- Public static void main (string [] ARGs ){
- String url = "http://www.google.co.kr/search? Hl = en & Q = % ed % 95% 9C % EA % B5 % ad % EC % 96% B4 + & btng = Google + SEARCH & AQ = F & OQ = ";
- System. Out. println (parseurlkeyword. getkeyword (URL ));
- System. Out. println ("");
- Url = "http://www.google.cn/search? Q = % E6 % 8f % 90% E5 % 8f % 96 + % E6 % 90% 9C % E7 % B4 % A2 % E5 % BC % 95% E6 % 93% 8e + % E5 % 85% b3 % E9 % 94% AE % E5 % ad % 97 & HL = ZH-CN & newwindow = 1 & SA = 2 ";
- System. Out. println (parseurlkeyword. getkeyword (URL ));
- System. Out. println ("");
- Url = "http://www.google.com.tw/search? Hl = ZH-CN & Q = % E6 % B9 % 98% E9 % 8B % BC % E4 % B8 % ad % E5 % 9C % 8B % E9 % a6 % 99% E7 % 85% 99 & btng = Google + % E6 % 90% 9C % E7 % B4 % A2 & AQ = F & OQ = ";
- System. Out. println (parseurlkeyword. getkeyword (URL ));
- System. Out. println ("");
- Url = "http://www.baidu.com/s? WD = % D6 % D0 % 87% F8 % D3 % D0 % be % 80% D8 % 9f % C8 % Ce % B9 % AB % CB % Be ";
- System. Out. println (parseurlkeyword. getkeyword (URL ));
- System. Out. println ("");
- Url = "http://www.baidu.com/s? WD = % C6 % F3 % D2 % B5 % Cd % C6 % B9 % E3 ";
- System. Out. println (parseurlkeyword. getkeyword (URL ));
- System. Out. println ("");
- }
- Public static string getkeyword (string URL ){
- String keywordreg = "(? : Yahoo. +? [//? | &] P = | openfind. +? Query = | Google. +? Q = | Lycos. +? Query = | onseek. +? Keyword = | search //. Tom. +? WORD = | search //. QQ //. com. +? WORD = | zhongsou //. com. +? WORD = | search //. MSN //. com. +? Q = | yisou //. com. +? P = | sina. +? WORD = | sina. +? Query = | sina. +? _ Searchkey = | Sohu. +? WORD = | Sohu. +? Key_word = | Sohu. +? Query = | 163. +? Q = | Baidu. +? WD = | Soso. +? W = | 3721 //. com. +? P = | alltheweb. +? Q =) ([^ &] *) ";
- String encodereg = "^ (? : [// X00-// x7f] | [// xfc-// xFF] [// X80-// xbf] {5} | [// xf8-// xfb] [// X80-// xbf] {4} | [// xf0-// xf7] [// X80-// xbf] {3} | [// xe0 -// XeF] [// X80-// xbf] {2} | [// xc0-// xdf] [// X80-// xbf]) + $ ";
- Pattern keywordpatt = pattern. Compile (keywordreg );
- Stringbuffer keyword = new stringbuffer (20 );
- Matcher keywordmat = keywordpatt. matcher (URL );
- While (keywordmat. Find ()){
- Keywordmat. appendreplacement (keyword, "$1 ");
- }
- If (! Keyword. tostring (). Equals ("")){
- String keywordstmp = keyword. tostring (). Replace ("http: // www .","");
- Pattern encodepatt = pattern. Compile (encodereg );
- String unescapestring = parseurlkeyword. Unescape (keywordstmp );
- Matcher encodemat = encodepatt. matcher (unescapestring );
- String encodestring = "GBK ";
- If (encodemat. Matches () encodestring = "UTF-8 ";
- Try {
- Return urldecoder. Decode (keywordstmp, encodestring );
- } Catch (unsupportedencodingexception e ){
- Return "";
- }
- }
- Return "";
- }
- Public static string Unescape (string SRC ){
- Stringbuffer TMP = new stringbuffer ();
- TMP. ensurecapacity (SRC. Length ());
- Int lastpos = 0, Pos = 0;
- Char ch;
- While (lastpos <SRC. Length ()){
- Pos = SRC. indexof ("%", lastpos );
- If (Pos = lastpos ){
- If (SRC. charat (Pos + 1) = 'U '){
- Ch = (char) integer. parseint (SRC. substring (Pos + 2, POS + 6), 16 );
- TMP. append (CH );
- Lastpos = POS + 6;
- } Else {
- Ch = (char) integer. parseint (SRC. substring (Pos + 1, POS + 3), 16 );
- TMP. append (CH );
- Lastpos = POS + 3;
- }
- } Else {
- If (Pos =-1 ){
- TMP. append (SRC. substring (lastpos ));
- Lastpos = SRC. Length ();
- } Else {
- TMP. append (SRC. substring (lastpos, POS ));
- Lastpos = Pos;
- }
- }
- }
- Return TMP. tostring ();
- }
- }
The following is the implementation code of ASP:
- Function decodeuri (s)
- S = Unescape (s)
- Dim Reg, CS
- Cs = "GBK"
- Set Reg = new Regexp
- Reg. pattern = "^ (? : [/X00-/x7f] | [/xfc-/xFF] [/X80-/xbf] {5} | [/xf8-/xfb] [/X80-/xbf] {4} | [/xf0-/xf7] [/X80-/xbf] {3} | [/xe0-/XeF] [/X80-/xbf] {2} | [/xc0-/xdf] [/X80-/xbf]) + $"
- If Reg. test (s) Then cs = "UTF-8"
- Set Reg = nothing
- Dim Sm
- Set Sm = Createobject ("ADODB. Stream ")
- With Sm
- . Type = 2
- . Mode = 3
- . Open
- . Charset = "iso-8859-1"
- . Writetext s
- . Position = 0
- . Charset = cs
- Decodeuri =. readtext (-1)
- . Close
- End
- Set Sm = nothing
- End Function
- Response. Write decodeuri ("% B8 % A7 % CB % B3 % C7 % E0 % CB % C9 % D2 % A9 % D2 % B5 ")
- Response. write decodeuri ("% E6 % 8A % 9A % E9 % a1 % Ba % E9 % 9d % 92% E6 % 9d % be % E8 % 8d % af % E4 % B8 % 9A")