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

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

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

http://www.google.com/search?hl=zh-CN&q=%E5%AD%A4%E7%8B%AC&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的转换.

下面主要实现代码.

ASP/Visual Basic Code 复制内容到剪贴板

  1. Public Function GetSearchKeyword(RefererUrl) '搜索关键词
  2. if RefererUrl= "" or len(RefererUrl)<1 then exit function
    1. on error resume next
    1. Dim re
  3. Set re = New RegExp
  4. re.IgnoreCase = True
  5. re.Global = True
  6. Dim a,b,j
  7. '模糊查找关键词,此方法速度较快,范围也较大
  8. re.Pattern = "(word=([^&])|q=([^&])|p=([^&])|query=([^&])|name=([^&])|_searchkey=([^&])|baidu.?w=([^&]))"
  9. Set a = re.Execute(RefererUrl)
  10. If a.Count>0 then
  11. Set b = a(a.Count-1).SubMatches
  12. For j=1 to b.Count
  13. If Len(b(j))>0 then
  14. if instr(1,RefererUrl, "google" ,1) then
  15. GetSearchKeyword=Trim(U8Decode(b(j)))
  16. elseif instr(1,refererurl, "yahoo" ,1) then
  17. GetSearchKeyword=Trim(U8Decode(b(j)))
  18. elseif instr(1,refererurl, "yisou" ,1) then
  19. GetSearchKeyword=Trim(getkey(b(j)))
  20. elseif instr(1,refererurl, "3721" ,1) then
  21. GetSearchKeyword=Trim(getkey(b(j)))
  22. else
  23. GetSearchKeyword=Trim(getkey(b(j)))
  24. end if
  25. Exit Function
  26. end if
  27. Next
  28. End If
  29. if err then
  30. err.clear
  31. GetSearchKeyword = RefererUrl
  32. else
  33. GetSearchKeyword = ""
  34. end if
  35. End Function
      1. Function URLEncoding(vstrIn)
  36. dim strReturn,i,thischr
  37. strReturn = ""
  38. For i = 1 To Len(vstrIn)
  39. ThisChr = Mid(vStrIn,i,1)
  40. If Abs(Asc(ThisChr)) < &HFF Then
  41. strReturn = strReturn & ThisChr
  42. Else
  43. innerCode = Asc(ThisChr)
  44. If innerCode < 0 Then
  45. innerCode = innerCode + &H10000
  46. End If
  47. Hight8 = (innerCode And &HFF00)\ &HFF
  48. Low8 = innerCode And &HFF
  49. strReturn = strReturn & "%" & Hex(Hight8) & "%" & Hex(Low8)
  50. End If
  51. Next
  52. URLEncoding = strReturn
  53. End Function
  54. function getkey(key)
  55. dim oReq
  56. set oReq = CreateObject( "MSXML2.XMLHTTP" )
  57. oReq.open "POST" , "http://" &WebUrl "/system/ShowGb2312XML.asp?a=" &key,false
  58. oReq.send
  59. getkey=UTF2GB(oReq.responseText)
  60. end function
  61. function chinese2unicode(Str)
  62. dim i
  63. dim Str_one
  64. dim Str_unicode
  65. for i=1 to len(Str)
  66. Str_one=Mid(Str,i,1)
  67. Str_unicode=Str_unicode&chr(38)
  68. Str_unicode=Str_unicode&chr(35)
  69. Str_unicode=Str_unicode&chr(120)
  70. Str_unicode=Str_unicode& Hex(ascw(Str_one))
  71. Str_unicode=Str_unicode&chr(59)
  72. next
  73. Response.Write Str_unicode
  74. end function
    1. function UTF2GB(UTFStr)
  75. Dim dig,GBSTR
  76. for Dig=1 to len(UTFStr)
  77. if mid(UTFStr,Dig,1)= "%" then
  78. if len(UTFStr) >= Dig+8 then
  79. GBStr=GBStr & ConvChinese(mid(UTFStr,Dig,9))
  80. Dig=Dig+8
  81. else
  82. GBStr=GBStr & mid(UTFStr,Dig,1)
  83. end if
  84. else
  85. GBStr=GBStr & mid(UTFStr,Dig,1)
  86. end if
  87. next
  88. UTF2GB=GBStr
  89. end function
      1. function ConvChinese(x)
  90. dim a,i,j,DigS, Unicode
  91. A=split(mid(x,2), "%" )
  92. i=0
  93. j=0
    1. for i=0 to ubound(A)
  94. A(i)=c16to2(A(i))
  95. next
    1. for i=0 to ubound(A)-1
  96. DigS=instr(A(i), "0" )
  97. Unicode = ""
  98. for j=1 to DigS-1
  99. if j=1 then
  100. A(i)=right(A(i),len(A(i))-DigS)
  101. Unicode = Unicode & A(i)
  102. else
  103. i=i+1
  104. A(i)=right(A(i),len(A(i))-2)
  105. Unicode = Unicode & A(i)
  106. end if
  107. next
    1. if len(c2to16( Unicode ))=4 then
  108. ConvChinese=ConvChinese & chrw(int( "&H" & c2to16( Unicode )))
  109. else
  110. ConvChinese=ConvChinese & chr(int( "&H" & c2to16( Unicode )))
  111. end if
  112. next
  113. end function
    1. function U8Decode(enStr)
  114. '输入一堆有%分隔的字符串,先分成数组,根据utf8规则来判断补齐规则
  115. '输入:关 E5 85 B3 键 E9 94 AE 字 E5 AD 97
  116. '输出:关 B9D8 键 BCFC 字 D7D6
  117. dim c,i,i2,v,deStr,WeiS
    1. for i=1 to len(enStr)
  118. c=Mid(enStr,i,1)
  119. if c= "%" then
  120. v=c16to2(Mid(enStr,i+1,2))
  121. '判断第一次出现0的位置,
  122. '可能是1(单字节),3(3-1字节),4,5,6,7不可能是2和大于7
  123. '理论上到7,实际不会超过3。
  124. WeiS=instr(v, "0" )
  125. v=right(v,len(v)-WeiS) '第一个去掉最左边的WeiS个
  126. i=i+3
  127. for i2=2 to WeiS-1
  128. c=c16to2(Mid(enStr,i+1,2))
  129. c=right(c,len(c)-2) '其余去掉最左边的两个
  130. v=v & c
  131. i=i+3
  132. next
  133. if len(c2to16(v)) =4 then
  134. deStr=deStr & chrw(c2to10(v))
  135. else
  136. deStr=deStr & chr(c2to10(v))
  137. end if
  138. i=i-1
  139. else
  140. if c= "+" then
  141. deStr=deStr " "
  142. else
  143. deStr=deStr&c
  144. end if
  145. end if
  146. next
  147. U8Decode = deStr
  148. end function
    1. function c16to2(x)
  149. '这个函数是用来转换16进制到2进制的,可以是任何长度的,一般转换UTF-8的时候是两个长度,比如A9
  150. '比如:输入“C2”,转化成“11000010”,其中1100是"c"是10进制的12(1100),那么2(10)不足4位要补齐成(0010)。
  151. dim tempstr
  152. dim i:i=0 '临时的指针
    1. for i=1 to len(trim(x))
  153. tempstr= c10to2(cint(int( "&h" & mid(x,i,1))))
  154. do while len(tempstr)<4
  155. tempstr= "0" & tempstr '如果不足4位那么补齐4位数
  156. loop
  157. c16to2=c16to2 & tempstr
  158. next
  159. end function

ASP/Visual Basic Code 复制内容到剪贴板

  1. function c2to16(x)
  2. '2进制到16进制的转换,每4个0或1转换成一个16进制字母,输入长度当然不可能不是4的倍数了
    1. dim i:i=1 '临时的指针
  3. for i=1 to len(x) step 4
  4. c2to16=c2to16 & hex(c2to10(mid(x,i,4)))
  5. next
  6. end function
    1. function c2to10(x)
  7. '单纯的2进制到10进制的转换,不考虑转16进制所需要的4位前零补齐。
  8. '因为这个函数很有用!以后也会用到,做过通讯和硬件的人应该知道。
  9. '这里用字符串代表二进制
  10. c2to10=0
  11. if x= "0" then exit function '如果是0的话直接得0就完事
  12. dim i:i=0 '临时的指针
  13. for i= 0 to len(x) -1 '否则利用8421码计算,这个从我最开始学计算机的时候就会,好怀念当初教我们的谢道建老先生啊!
  14. if mid(x,len(x)-i,1)= "1" then c2to10=c2to10+2^(i)
  15. next
  16. end function
    1. function c10to2(x)
  17. '10进制到2进制的转换
  18. dim sign, result
  19. result = ""
  20. '符号
  21. sign = sgn(x)
  22. x = abs(x)
  23. if x = 0 then
  24. c10to2 = 0
  25. exit function
  26. end if
  27. do until x = "0"
  28. result = result & (x mod 2)
  29. x = x \ 2
  30. loop
  31. result = strReverse(result)
  32. if sign = -1 then
  33. c10to2 = "-" & result
  34. else
  35. c10to2 = result
  36. end if
  37. end function
    1. function URLDecode(enStr)
  38. dim deStr,strSpecial
  39. dim c,i,v
  40. deStr= ""
  41. strSpecial= "!" "#$%&'()*+,/:;<=>?@[\]^`{ |}~%"
  42. for i=1 to len(enStr)
  43. c=Mid(enStr,i,1)
  44. if c= "%" then
  45. v=eval( "&h" +Mid(enStr,i+1,2))
  46. if inStr(strSpecial,chr(v))>0 then
  47. deStr=deStr&chr(v)
  48. i=i+2
  49. else
  50. v=eval( "&h" +Mid(enStr,i+1,2)+Mid(enStr,i+4,2))
  51. deStr=deStr&chr(v)
  52. i=i+5
  53. end if
  54. else
  55. if c= "+" then
  56. deStr=deStr " "
  57. else
  58. deStr=deStr&c
  59. end if
  60. end if
  61. next
  62. URLDecode=deStr
  63. end function

许多代码都是网上的.找不到作者.

PS:现在暑假就要接受,由于家庭原因我不想留在我的城市.中考到达本地重点.不想说城市名字.否则会招来熟人.只要不在山东的学校算是重点的能不能联系下.

对程序有极大兴趣,但信息奥赛只活得一等的X名.因为我认为技术不应该在所谓竞赛中体现,就如才能不应该在那些无意义的考试中体现一样.电子作品也弄了各省一等..不过也一般.学习一般...所以只要是一般重点就好了..只是不想在离家太近的地方.

现在asp十分熟练,虽然有些知识缺陷,比如编码问题(汗...),但是网络如此大,我想我不是只有在课本中才能得到所谓的知识.而且现在正在啃asp.net的书,如果贵校做网站完全可以帮忙.

对新技术十分狂热,虽然被他们称为审美有障碍的人.但我想看到结构偶的程序还不至于吐血.

算了..再贴点.

偶开发D Database+asp ->xml+xslt->xhtml +css 的算是叫CMS的东西

http://www.joysou.com

也用了CSDN用的FCK编辑器,今天上来才发现换了.不过那个FCK的FIle系统让偶统统改掉.

这个系统在暑假结束前一定会发布.不过很多朋友说易用性有问题...很多人不会xslt.汗...

Published At
Categories with Web编程
Tagged with
comments powered by Disqus