最近在公司网站的时候需要将公司新浪博客的内容在当前网站中显示,用IFRAME肯定不行,不服合网站的风格。于是自己使用了XMLHTTP来获取公司博客列表页中的标题和URL,再依次读取URL中的代码并截取内容部分。主要使用的方法有ASP中的getHTTPPage,spilt。
关键代码:
<%
if application("SinaBlog")<>"" then
response.write application("SinaBlog")
else
Function getHTTPPage(url)
set http=Server.createobject("Microsoft.XMLHTTP")
Http.open "GET",url,false
Http.send()
if Http.readystate<>4 then
exit function
end if
getHTTPPage=bytesToBSTR(Http.responseBody,"utf-8")'此处注意使用utf-8编码
set http=nothing
If Err.number<>0 then
Response.Write "代码获取失败"
Err.Clear
End If
End Function
Function BytesToBstr(body,Cset)
dim objstream
set objstream = Server.CreateObject("adodb.stream")
objstream.Type = 1
objstream.Mode =3
objstream.Open
objstream.Write body
objstream.Position = 0
objstream.Type = 2
objstream.Charset = Cset
BytesToBstr = objstream.ReadText
objstream.Close
set objstream = nothing
End Function
ListUrl="http://blog.sina.com.cn/s/articlelist_1622797951_0_1.html"'新浪博客列表页地址
if ListUrl="" Then response.write "url is empty":response.end
startFlag="<div class=""componentContent"">"
endFlag="<div class=""bottom_blk"">"
if application("urldata")<>"" Then
strContent=application("urldata")
else
strContent=getHTTPPage(ListUrl)
application("urldata")=strContent
end if
if strContent="" or instr(strContent,startFlag)<1 Then
response.write "code get error"
else
c1=split(strContent,startFlag)
c2=split(c1(1),endFlag)
MainData=c2(0)'内容列表
aSplitstr="<div class=""manage floatRight""></div>"'文章分割符
c3=split(MainData,aSplitstr)
if ubound(c3)>6 then'最多只显示6条
MaxTitNum=6
else
MaxTitNum=ubound(c3)
end if
for i=0 to MaxTitNum'开始分个读取所需数据
if instr(c3(i),"<div class=""articleTitle_d"">")>0 then'判断是否为所需数据
c4=split(c3(i),"target=""_blank"">")'读取文章URL
c5=split(c4(0),"<a href=")
a_url=replace(c5(1),"""","")'赋值URL
c6=split(c4(1),"</a>")
a_tit=c6(0)'赋值文章标题
c7=split(c4(1),"<span class=""time space_d01"">")
a_pot=replace(replace(replace(c7(1),"</span></div>",""),"(",""),")","")
a_body=getHTTPPage(a_url)
if a_body="" or instr(a_body,"<div class=""articleContent"" id=""articleBody"">")<1 then
a_con="get content error"
else
a_con1=split(a_body,"<div class=""articleContent"" id=""articleBody"">")'分割文章body
a_con2=split(a_con1(1),"<div class=""toMiniblog"">")
a_con=replace(replace(a_con2(0),"<p> <wbr></P>",""),"<!-- -->","")'赋值文章内容
end if
application("SinaBlog")=application("SinaBlog")&"<div class=title>"&a_tit&"</div>"&_
"<div class=content>"&a_con&" <a href="&a_url&" target=_blank>...<b>点击查看全文</b></a></div> <div class=posttime>发表于:"&a_pot&"</div>"
response.write application("SinaBlog")
end if
next
end if
end if%>
原创文章转载请注明引自石头博客 http://www.stou.info/ 欢迎订阅石头博客。
感觉写成插件一定会有很多人喜欢.