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

时间:2021-05-28

所以必须要通过编码后的关键字,例如“解析关键字编码”在google里面输入搜索,得到编码后的“%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代码的实现:
复制代码 代码如下:
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://pile(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();
}
}

以下是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 With
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")

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章