获取网站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,",","")
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")%>
轉載于:https://blog.51cto.com/naiht/245487
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的获取网站Alexa排名数值的方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 白天黑夜都在“肝”,肝的情况你真的了解吗
- 下一篇: 25岁小伙做鼻子整形失败毁容!