Java and ASP analyze Keywords of various search engines and automatically recognize the encoding of keywords in URLs

Source: Internet
Author: User

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:

  1. Package test;
  2. Import java. Io. unsupportedencodingexception;
  3. Import java.net. urldecoder;
  4. Import java. util. RegEx. matcher;
  5. Import java. util. RegEx. pattern;
  6. Public class parseurlkeyword {
  7. Public static void main (string [] ARGs ){
  8. 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 = ";
  9. System. Out. println (parseurlkeyword. getkeyword (URL ));
  10. System. Out. println ("");
  11. 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 ";
  12. System. Out. println (parseurlkeyword. getkeyword (URL ));
  13. System. Out. println ("");
  14. 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 = ";
  15. System. Out. println (parseurlkeyword. getkeyword (URL ));
  16. System. Out. println ("");
  17. Url = "http://www.baidu.com/s? WD = % D6 % D0 % 87% F8 % D3 % D0 % be % 80% D8 % 9f % C8 % Ce % B9 % AB % CB % Be ";
  18. System. Out. println (parseurlkeyword. getkeyword (URL ));
  19. System. Out. println ("");
  20. Url = "http://www.baidu.com/s? WD = % C6 % F3 % D2 % B5 % Cd % C6 % B9 % E3 ";
  21. System. Out. println (parseurlkeyword. getkeyword (URL ));
  22. System. Out. println ("");
  23. }
  24. Public static string getkeyword (string URL ){
  25. 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 =) ([^ &] *) ";
  26. 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]) + $ ";
  27. Pattern keywordpatt = pattern. Compile (keywordreg );
  28. Stringbuffer keyword = new stringbuffer (20 );
  29. Matcher keywordmat = keywordpatt. matcher (URL );
  30. While (keywordmat. Find ()){
  31. Keywordmat. appendreplacement (keyword, "$1 ");
  32. }
  33. If (! Keyword. tostring (). Equals ("")){
  34. String keywordstmp = keyword. tostring (). Replace ("http: // www .","");
  35. Pattern encodepatt = pattern. Compile (encodereg );
  36. String unescapestring = parseurlkeyword. Unescape (keywordstmp );
  37. Matcher encodemat = encodepatt. matcher (unescapestring );
  38. String encodestring = "GBK ";
  39. If (encodemat. Matches () encodestring = "UTF-8 ";
  40. Try {
  41. Return urldecoder. Decode (keywordstmp, encodestring );
  42. } Catch (unsupportedencodingexception e ){
  43. Return "";
  44. }
  45. }
  46. Return "";
  47. }
  48. Public static string Unescape (string SRC ){
  49. Stringbuffer TMP = new stringbuffer ();
  50. TMP. ensurecapacity (SRC. Length ());
  51. Int lastpos = 0, Pos = 0;
  52. Char ch;
  53. While (lastpos <SRC. Length ()){
  54. Pos = SRC. indexof ("%", lastpos );
  55. If (Pos = lastpos ){
  56. If (SRC. charat (Pos + 1) = 'U '){
  57. Ch = (char) integer. parseint (SRC. substring (Pos + 2, POS + 6), 16 );
  58. TMP. append (CH );
  59. Lastpos = POS + 6;
  60. } Else {
  61. Ch = (char) integer. parseint (SRC. substring (Pos + 1, POS + 3), 16 );
  62. TMP. append (CH );
  63. Lastpos = POS + 3;
  64. }
  65. } Else {
  66. If (Pos =-1 ){
  67. TMP. append (SRC. substring (lastpos ));
  68. Lastpos = SRC. Length ();
  69. } Else {
  70. TMP. append (SRC. substring (lastpos, POS ));
  71. Lastpos = Pos;
  72. }
  73. }
  74. }
  75. Return TMP. tostring ();
  76. }
  77. }

 

The following is the implementation code of ASP:

  1. Function decodeuri (s)
  2. S = Unescape (s)
  3. Dim Reg, CS
  4. Cs = "GBK"
  5. Set Reg = new Regexp
  6. 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]) + $"
  7. If Reg. test (s) Then cs = "UTF-8"
  8. Set Reg = nothing
  9. Dim Sm
  10. Set Sm = Createobject ("ADODB. Stream ")
  11. With Sm
  12. . Type = 2
  13. . Mode = 3
  14. . Open
  15. . Charset = "iso-8859-1"
  16. . Writetext s
  17. . Position = 0
  18. . Charset = cs
  19. Decodeuri =. readtext (-1)
  20. . Close
  21. End
  22. Set Sm = nothing
  23. End Function
  24. Response. Write decodeuri ("% B8 % A7 % CB % B3 % C7 % E0 % CB % C9 % D2 % A9 % D2 % B5 ")
  25. Response. write decodeuri ("% E6 % 8A % 9A % E9 % a1 % Ba % E9 % 9d % 92% E6 % 9d % be % E8 % 8d % af % E4 % B8 % 9A")

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.