您当前的位置: 首页 > 网站编程 > ASP教程 > 获取网站Alexa排名数值的方法

获取网站Alexa排名数值的方法

作者:admin 来源:不详 发布时间: 2008-06-04 08:50 点击:
网络上有很多文章介绍如何自动抓取网站的Alexa排名,但是仔细一看发现抓取到的数据(Alexa排名数值)被Alexa加入了很多干扰元素,如果只是要将数据(Alexa排名数值)显示在页面倒没有什么问题,若是要对数据进行处理比如将两个网站的排名数值进行比较或者将网

获取网站Alexa排名数值的方法

    网络上有很多文章介绍如何自动抓取网站的Alexa排名,但是仔细一看发现抓取到的数据(Alexa排名数值)被Alexa加入了很多干扰元素,如果只是要将数据(Alexa排名数值)显示在页面倒没有什么问题,若是要对数据进行处理比如将两个网站的排名数值进行比较或者将网站排名数值存入数据库供日后调用,则要对抓取到的数据进行适当的处理。 
    以下是本人结合其他网友提供的代码,对抓取到的数据进行处理后获得干干净净的数值的方法。核心 
函数代码如下: 
<% 
’// alexa 世界排名的查询页面为:http://www.alexa.com/data/details/traffic_details?q=&Url=
’// 以下函数抓取到含有干扰元素的数据并通过函数对数据进行处理,获得干干净净的Alexa排名数值 
Function alexa(str) 
url="http://www.alexa.com/data/details/traffic_details?q=&url="&str 
strs=str 
If IsObjInstalled("AspHTTP.Conn")=true Then 
str= getaspHTTPPage(url) 
else 
str= getHTTPPage(url) 
End if 

if str="" then 
Call Error() 
else 
str_=str 
str1="" 
set reg=new Regexp 
reg.Multiline=True 
reg.Global=True 
reg.IgnoreCase=true 
str_top="<!--Did you know" 
str_bottom="</span>" 
reg.Pattern=""&str_top&"((.|\n)*?)"&str_bottom&"" 
Set matches = reg.execute(str_) 
str1="" 
For Each match1 in matches 
str1=str1&match1.Value&"***" 
Next 
Set matches = Nothing 
Set reg = Nothing 
IF str1 <> "" Then 
str1 = Replace(str1,"<!--Did you know? Alexa offers this data programmatically. 
Visit http://webservices.amazon.com/ for more information about the Alexa Web Information 
Service.-->","") 
str1 = Replace(str1,"</span>","") 
Str_11=split(str1,"<div class=""borderBottom""></div>") 
str1 = Str_11(0) 
Str_11 = split(str1,"***") 
str1_Pan = Str_11(0) 
End If 
set reg=new Regexp 
reg.Multiline=True 
reg.Global=True 
reg.IgnoreCase=true 
str_top="<td class=""traffic"">" 
str_bottom="</td>" 
reg.Pattern=""&str_top&"((.|\n)*?)"&str_bottom&"" 
Set matches = reg.execute(str_) 
str1="" 
For Each match1 in matches 
str1=str1&match1.Value&"***" 
Next 
Set matches = Nothing 
Set reg = Nothing 
IF str1 <> "" Then 
Str_11=split(str1,"***") 
End If 
End if 
’************************************ 
’************************************ 
alexa=getcorrectvalue(str1_Pan) 
’************************************ 
’************************************ 
End Function 

’************************************ 
’此功能函数去除干扰元素 
’************************************ 
function getcorrectvalue(source) 
source="|"+source+"|" 
while InStr(source,"<")>0 
thestart = InStr(source, "<") 
theend = InStr(source, ">") 
source = mid(source,1,thestart-1)+right(source,(len(source)-theend)) 
wend 
source=replace(source,"|","") 
source=replace(source,",","") 
2 获取网站Alexa排名数值的方法 
getcorrectvalue=source 
end function 

’************************************ 
’************************************ 

’// <summary> 
’// 采用 Microsoft.XMLHTTP 组件采集数据 
’// </summary> 
Function getHTTPPage(url) 
on error resume next 
dim http 
set http=Server.createobject("Microsoft.XMLHTTP") 
Http.open "GET",url,false 
Http.send() 
if Http.readystate<>4 then 
exit function 
end if 
getHTTPPage=bytes2BSTR(Http.responseBody) 
set http=nothing 
if err.number<>0 then err.Clear 
End function 
’// <summary> 
’// 采用 ADODB.Stream 处理采集到的数据,把二进制的文件转成文本字符 
’// </summary> 
Function Bytes2bStr(vin) 
Dim BytesStream,StringReturn 
Set BytesStream = Server.CreateObject("ADODB.Stream") 
BytesStream.Type = 2 
BytesStream.Open 
BytesStream.WriteText vin 
BytesStream.Position = 0 
BytesStream.Charset = "GB2312" 
BytesStream.Position = 2 
StringReturn =BytesStream.ReadText 
BytesStream.close 
Set BytesStream = Nothing 
Bytes2bStr = StringReturn 
End Function 

’// <summary> 
’// 采用 AspHTTP.Conn 组件采集数据 
’// </summary> 
Function getaspHTTPPage(url) 
if url="" then 
exit function 
end if 
Set HttpObj = Server.CreateObject("AspHTTP.Conn") 
’设置代理服务器,通过代理上网的用户需要设置此选项 
If ProxyIP=1 Then 
HttpObj.Proxy="192.168.5.254:808" 
end if 
HTTPObj.TimeOut = 45 
HttpObj.Url = url 
HttpObj.RequestMethod = "GET" 
getaspHTTPPage = HttpObj.GetURL 
set HttpObj=nothing 
End function 


’//<summary> 
’//检查组件,采用xmlhttp抓取网页还是AspHTTP 
’//</summary> 
Function IsObjInstalled(strClassString) 
On Error Resume Next 
IsObjInstalled = False 
Err = 0 
Dim xTestObj 
Set xTestObj = Server.CreateObject(strClassString) 
If 0 = Err Then 
If AspHttpOpen=1 Then 
IsObjInstalled = True 
’Response.write "当前组件 ASPHTTP" 
Else 
IsObjInstalled = False 
’Response.write "当前组件 XMLHTTP" 
End If 
Else 
IsObjInstalled = False 
’Response.write "当前组件 XMLHTTP" 
End If 
Set xTestObj = Nothing 
Err = 0 
End Function 
Sub Error() 
response.write "<BR> 抓取不到数据-可能是因为网络原因不能访问站点<BR><a 
href=javascript:location.reload();>重试</a>" 
response.end 
End Sub 

%> 

调用方法: 

<% 
response.write alexa("http://blog.sina.com.cn/u/1086421675") 
%> 
<%=alexa("http://blog.sina.com.cn/u/1086421675")%> 
分享到:
本文"获取网站Alexa排名数值的方法"由远航站长收集整理而来,仅供大家学习与参考使用。更多网站制作教程尽在远航站长站。
顶一下
(0)
0%
踩一下
(0)
0%
[点击 次] [返回上一页] [打印]
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 密码: 验证码:
关于本站 - 联系我们 - 网站声明 - 友情连接- 网站地图 - 站点地图 - 返回顶部
Copyright © 2007-2013 www.yhzhan.com(远航站长). All Rights Reserved .
远航站长:为中小站长提供最佳的学习与交流平台,提供网页制作与网站编程等各类网站制作教程.
官方QQ:445490277 网站群:26680406 网站备案号:豫ICP备07500620号-4