欢迎光临
我们一直在努力

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

建站超值云服务器,限时71元/月

不知道为什么现在各大搜索引擎编码居然不一样.当然不是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的转换.

下面主要实现代码.

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
  dim c,i,i2,v,destr,weis

  for i=1 to len(enstr)
    c=mid(enstr,i,1)
    if c=”%” then
      v=c16to2(mid(enstr,i+1,2))
      判断第一次出现0的位置,
      可能是1(单字节),3(3-1字节),4,5,6,7不可能是2和大于7
      理论上到7,实际不会超过3。
      weis=instr(v,”0″)
      v=right(v,len(v)-weis)第一个去掉最左边的weis个
      i=i+3
      for i2=2 to weis-1
        c=c16to2(mid(enstr,i+1,2))
        c=right(c,len(c)-2)其余去掉最左边的两个
        v=v & c
        i=i+3
      next
      if len(c2to16(v)) =4 then
        destr=destr & chrw(c2to10(v))
      else
        destr=destr & chr(c2to10(v))
      end if
      i=i-1
    else
      if c=”+” then
        destr=destr&” “
      else
        destr=destr&c
      end if
    end if
  next
  u8decode = destr
end function

function c16to2(x)
 这个函数是用来转换16进制到2进制的,可以是任何长度的,一般转换utf-8的时候是两个长度,比如a9
 比如:输入“c2”,转化成“11000010”,其中1100是”c”是10进制的12(1100),那么2(10)不足4位要补齐成(0010)。
 dim tempstr
 dim i:i=0临时的指针

 for i=1 to len(trim(x))
  tempstr= c10to2(cint(int(“&h” & mid(x,i,1))))
  do while len(tempstr)<4
   tempstr=”0″ & tempstr如果不足4位那么补齐4位数
  loop
  c16to2=c16to2 & tempstr
 next
end function

function c2to16(x)
  2进制到16进制的转换,每4个0或1转换成一个16进制字母,输入长度当然不可能不是4的倍数了

  dim i:i=1临时的指针
  for i=1 to len(x)  step 4
   c2to16=c2to16 & hex(c2to10(mid(x,i,4)))
  next
end function

function c2to10(x)
  单纯的2进制到10进制的转换,不考虑转16进制所需要的4位前零补齐。
  因为这个函数很有用!以后也会用到,做过通讯和硬件的人应该知道。
  这里用字符串代表二进制
   c2to10=0
   if x=”0″ then exit function如果是0的话直接得0就完事
   dim i:i=0临时的指针
   for i= 0 to len(x) -1否则利用8421码计算,这个从我最开始学计算机的时候就会,好怀念当初教我们的谢道建老先生啊!
    if mid(x,len(x)-i,1)=”1″ then c2to10=c2to10+2^(i)
   next
end function

function c10to2(x)
10进制到2进制的转换
  dim sign, result
  result = “”
  符号
  sign = sgn(x)
  x = abs(x)
  if x = 0 then
    c10to2 = 0
    exit function
  end if
  do until x = “0”
    result = result & (x mod 2)
    x = x \ 2
  loop
  result = strreverse(result)
  if sign = -1 then
    c10to2 = “-” & result
  else
    c10to2 = result
  end if
end function

function urldecode(enstr)
  dim  destr,strspecial
  dim  c,i,v
  destr=””
  strspecial=”!””#$%&()*+,/:;<=>?@[\]^`{ |}~%”
  for  i=1  to  len(enstr)
    c=mid(enstr,i,1)
    if  c=”%”  then
    v=eval(“&h”+mid(enstr,i+1,2))
    if  instr(strspecial,chr(v))>0  then
    destr=destr&chr(v)
    i=i+2
    else
    v=eval(“&h”+mid(enstr,i+1,2)+mid(enstr,i+4,2))
    destr=destr&chr(v)
    i=i+5
    end  if
    else
    if  c=”+”  then
    destr=destr&” “
    else
    destr=destr&c
    end  if
    end  if
  next
  urldecode=destr
end function

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

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

qq:32113739

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

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

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

算了..再贴点.

偶开发d database+asp ->xml+xslt->xhtml +css 的算是叫cms的东西

http://www.joysou.com

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

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

唉…如果找不到学校.我也许会漂泊,也许会消失吧.当然这不是威胁..只是恨我的城市,恨那里看到的,干过的一切.

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » asp实现关键词获取(各搜索引擎,gb2312及utf-8)-ASP教程,ASP技巧
分享到: 更多 (0)