asp实现关键词获取(各搜索引擎,gb2312及utf-8)

2008-02-23 05:31:45来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折

不知道为什么现在各大搜索引擎编码居然不一样.当然不是gb2312就是utf-8了.编码问题是比较头疼的问题...头疼的不要命...

我们获得关键词,一般是通过来访页面的url进行分析的.比如

http://www.google.com/search?hl=zh-CN&q=瀛ょ嫭&lr=

各位肯定知道这个是通过urlencode编码的.

我们得到其中的信息,需要进行2步.第一步是进行urldecode,在我们普通参数活得的时候,这个是由asp自己来进行的,但是现在我们不得不进行手工解码.

网上函数很多,但都是针对于gb2312页面解gb2312.utf-8的.对于这个,我们可以很轻松的先进行解码,然后根据搜索引擎判断它的编码,如果是utf-8就再转换为gb2312.

但是由于我的网站是utf-8页面的.而utf-8页面我找到的只有解utf-8字符的urldecode编码的.在这里停顿了很久,最后我只能用最糟糕的方法,把拆分出来的关键词用xmlhttp提交到一个gb2312的asp页面,然后活得乱码(gb2312)后再进行gb2312 to utf-8的转换.

下面主要实现代码.

Public Function GetSearchKeyword(RefererUrl) '搜索关键词
if RefererUrl="" or len(RefererUrl)<1 then exit function

on error resume next

Dim re
Set re = New RegExp
re.IgnoreCase = True
re.Global = True
Dim a,b,j
'模糊查找关键词,此方法速度较快,范围也较大
re.Pattern = "(word=([^&]*)|q=([^&]*)|p=([^&]*)|query=([^&]*)|name=([^&]*)|_searchkey=([^&]*)|baidu.*?w=([^&]*))"
Set a = re.Execute(RefererUrl)
If a.Count>0 then
Set b = a(a.Count-1).SubMatches
For j=1 to b.Count
If Len(b(j))>0 then
if instr(1,RefererUrl,"google",1) then
GetSearchKeyword=Trim(U8Decode(b(j)))
elseif instr(1,refererurl,"yahoo",1) then
GetSearchKeyword=Trim(U8Decode(b(j)))
elseif instr(1,refererurl,"yisou",1) then
GetSearchKeyword=Trim(getkey(b(j)))
elseif instr(1,refererurl,"3721",1) then
GetSearchKeyword=Trim(getkey(b(j)))
else
GetSearchKeyword=Trim(getkey(b(j)))
end if
Exit Function
end if
Next
End If
if err then
err.clear
GetSearchKeyword = RefererUrl
else
GetSearchKeyword = ""
end if
End Function


Function URLEncoding(vstrIn)
dim strReturn,i,thischr
strReturn = ""
For i = 1 To Len(vstrIn)
ThisChr = Mid(vStrIn,i,1)
If Abs(Asc(ThisChr)) < &HFF Then
strReturn = strReturn & ThisChr
Else
innerCode = Asc(ThisChr)
If innerCode < 0 Then
innerCode = innerCode &H10000
End If
Hight8 = (innerCode And &HFF00)\ &HFF
Low8 = innerCode And &HFF
strReturn = strReturn & "%" & Hex(Hight8) & "%" & Hex(Low8)
End If
Next
URLEncoding = strReturn
End Function
function getkey(key)
dim oReq
set oReq = CreateObject("MSXML2.XMLHTTP")
oReq.open "POST","http://"&WebUrl&"/system/ShowGb2312XML.asp?a="&key,false
oReq.send
getkey=UTF2GB(oReq.responseText)
end function
function chinese2unicode(Str)
dim i
dim Str_one
dim Str_unicode
for i=1 to len(Str)
Str_one=Mid(Str,i,1)
Str_unicode=Str_unicode&chr(38)
Str_unicode=Str_unicode&chr(35)
Str_unicode=Str_unicode&chr(120)
Str_unicode=Str_unicode& Hex(ascw(Str_one))
Str_unicode=Str_unicode&chr(59)
next
Response.Write Str_unicode
end function

function UTF2GB(UTFStr)
Dim dig,GBSTR
for Dig=1 to len(UTFStr)
if mid(UTFStr,Dig,1)="%" then
if len(UTFStr) >= Dig 8 then
GBStr=GBStr & ConvChinese(mid(UTFStr,Dig,9))
Dig=Dig 8
else
GBStr=GBStr & mid(UTFStr,Dig,1)
end if
else
GBStr=GBStr & mid(UTFStr,Dig,1)
end if
next
UTF2GB=GBStr
end function


function ConvChinese(x)
dim a,i,j,DigS,Unicode
A=split(mid(x,2),"%")
i=0
j=0

for i=0 to ubound(A)
A(i)=c16to2(A(i))
next

for i=0 to ubound(A)-1
DigS=instr(A(i),"0")
Unicode=""
for j=1 to DigS-1
if j=1 then
A(i)=right(A(i),len(A(i))-DigS)
Unicode=Unicode & A(i)
else
i=i 1
A(i)=right(A(i),len(A(i))-2)
Unicode=Unicode & A(i)
end if
next

if len(c2to16(Unicode))=4 then
ConvChinese=ConvChinese & chrw(int("&H" & c2to16(Unicode)))
else
ConvChinese=ConvChinese & chr(int("&H" & c2to16(Unicode)))
end if
next
end function

function U8Decode(enStr)
'输入一堆有%分隔的字符串,先分成数组,根据utf8规则来判断补齐规则
'输入:关 E5 85 B3 键 E9 94 AE 字 E5 AD 97
'输出:关 B9D8 键 BCFC 字 D7D6

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:用ASP读INI配置文件的函数

下一篇:xmlhttp组件获取远程文件并筛选出目标数据