java, asp 分析各种搜索引擎的关键字,自动识别url 中关键字的编码

129 篇文章 1 订阅
61 篇文章 1 订阅

网上也有一些代码,大部分都是通过输入的关键字来识别编码,并解码。但是搜索引擎得到的referer来源地址上的关键字是通过URLencode编码过的,而且各个网站的关键字Urlencode编码都不一样,gbk,utf-8,gb2312等等。

所以必须要通过编码后的关键字,例如“解析关键字编码”在google里面输入搜索,得到编码后的“%E8%A7%A3%E6%9E%90%E5%85%B3%E9%94%AE%E5%AD%97%E7%BC%96%E7%A0%81”
链接地址为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.从以上地址中解析出关键字部分。
2.通过编码后的关键字获取编码时的编码名称(如:gbk,utf-8等等)

3.用URLdecode(keywords,encodeCode)来解码得到对应的关键字。

以下是java代码的实现:

  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.     
  25.   public static String getKeyword(String url){
  26.         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=)([^&]*)";
  27.         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])+$";
  28.         
  29.         Pattern keywordPatt = Pattern.compile(keywordReg);
  30.         StringBuffer keyword = new StringBuffer(20);
  31.         Matcher keywordMat = keywordPatt.matcher(url);
  32.         while (keywordMat.find()) {
  33.             keywordMat.appendReplacement(keyword, "$1");
  34.         }
  35.         if (!keyword.toString().equals("")){
  36.             String keywordsTmp = keyword.toString().replace("http://www.""");         
  37.             Pattern encodePatt = Pattern.compile(encodeReg);
  38.             String unescapeString = ParseURLKeyword.unescape(keywordsTmp);
  39.             Matcher encodeMat = encodePatt.matcher(unescapeString);
  40.             String encodeString = "gbk";
  41.             if (encodeMat.matches()) encodeString = "utf-8";
  42.             try {
  43.                 return URLDecoder.decode(keywordsTmp, encodeString);
  44.             } catch (UnsupportedEncodingException e) {
  45.                 return "";
  46.             }
  47.         }
  48.         return "";
  49.     }
  50.     
  51.     public static String unescape(String src) {
  52.         StringBuffer tmp = new StringBuffer();
  53.         tmp.ensureCapacity(src.length());
  54.         int lastPos = 0, pos = 0;
  55.         char ch;
  56.         while (lastPos < src.length()) {
  57.             pos = src.indexOf("%", lastPos);
  58.             if (pos == lastPos) {
  59.                 if (src.charAt(pos + 1) == 'u') {
  60.                     ch = (char) Integer.parseInt(src.substring(pos + 2, pos + 6), 16);
  61.                     tmp.append(ch);
  62.                     lastPos = pos + 6;
  63.                 } else {
  64.                     ch = (char) Integer.parseInt(src.substring(pos + 1, pos + 3), 16);
  65.                     tmp.append(ch);
  66.                     lastPos = pos + 3;
  67.                 }
  68.             } else {
  69.                 if (pos == -1) {
  70.                     tmp.append(src.substring(lastPos));
  71.                     lastPos = src.length();
  72.                 } else {
  73.                     tmp.append(src.substring(lastPos, pos));
  74.                     lastPos = pos;
  75.                 }
  76.             }
  77.         }
  78.         return tmp.toString();
  79.     }
  80. }

 

以下是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 With     
  22.     Set sm = Nothing     
  23. End Function     
  24.   
  25. Response.Write DecodeURI("%B8%A7%CB%B3%C7%E0%CB%C9%D2%A9%D2%B5")   
  26. Response.Write DecodeURI("%E6%8A%9A%E9%A1%BA%E9%9D%92%E6%9D%BE%E8%8D%AF%E4%B8%9A")
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值