猿问

请问在asp中,为什么mid()函数有时出错,有时不会出错?

错误类型:
Microsoft VBScript 运行时错误 (0x800A0005)
无效的过程调用或参数: 'Mid'
/zhuaqu1.asp, 第 51 行

我的程序如下,

<%@language=vbscript%>
<!--#include file=conn22.asp-->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>新建网页 1</title>
</head>
<body>
<%
function getHTTPPage(url) '通过组件Msxml2.XMLHTTP抓取网页内容
on error resume next 
dim http 
set http=Server.createobject("Msxml2.XMLHTTP") 
Http.open "get",url,false
Http.send() 
if Http.readystate<>4 then exit function 
getHTTPPage=bytes2BSTR(Http.responseBody) 
set http=nothing 
if err.number<>0 then err.Clear 
end function

Function bytes2BSTR(vIn) '把抓取到的内容转化为文本
dim strReturn 
dim i,ThisCharCode,NextCharCode 
strReturn = "" 
For i = 1 To LenB(vIn) 
ThisCharCode = AscB(MidB(vIn,i,1)) 
If ThisCharCode<&H80 Then 
strReturn = strReturn & Chr(ThisCharCode) 
Else 
NextCharCode = AscB(MidB(vIn,i+1,1)) 
strReturn = strReturn & Chr(CLng(ThisCharCode) * &H100 + CInt(NextCharCode)) 
i = i + 1 
End If 
Next 
bytes2BSTR = strReturn 
End Function

dim xurl
xurl="http://sx.zsedu.net/"
strContent=getHTTPPage(xurl)

strContent=Replace(strContent," ","")
pos1=1
do while pos1 > 0
%>
<%
pos1 = InStr(1, strContent, "<", vbTextCompare)
pos2 = InStr(1, strContent, ">", vbTextCompare)
if pos1 > 0 and pos2 > 1 then
title = Mid( strContent, pos1, pos2 - pos1+1 )
strContent=Replace(strContent,title,"")
end if
%>
<%
loop 
%>
<%
strContent=Replace(strContent," ","")
response.write(strContent)
dim visitors
whichfile=server.mappath("zhuaqu\zhuaqu.txt")
set qfs=server.createobject("Scripting.FileSystemObject")
set thisfile=qfs.opentextfile(whichfile)
visitors=thisfile.readline
thisfile.close
visitors=strContent
visitors=Replace(visitors," ","")

set out=qfs.createtextfile(whichfile)
out.writeline(visitors)
out.close
set fs=nothing

strtable="b"
set rs=server.createobject("adodb.recordset")
rs.open strtable,conn22,1,3
rs.addnew
rs("pinming")=strContent
rs("huohao")="11"
rs("jiage")="sa"
rs("miaosu")="df"
rs.update

rs.close
set rs=nothing

%>

</body>
</html>


潇潇雨雨
浏览 371回答 2
2回答

翻翻过去那场雪

我觉得我是找到原因了,http://sx.zsedu.net/页面内容里有单独出现的">",如语句:if(dypopLayer.filters.Alpha.opacity>0)中就含有单独的">",故pos1与pos2并不是如你想像中那样完全配套的!修改也很简单,程序不需要太大变化,只需要把pos1=1do while pos1 > 0%><%pos1 = InStr(1, strContent, "<", vbTextCompare)pos2 = InStr(1, strContent, ">", vbTextCompare)if pos1 > 0 and pos2 > 1 thentitle = Mid( strContent, pos1, pos2 - pos1+1 )strContent=Replace(strContent,title,"")end if%><%loop%>改为:pos1=1pos2=1do while pos1 > 0%><%pos1 = InStr(1, strContent, "<", vbTextCompare)pos2 = InStr(pos2+1, strContent, ">", vbTextCompare)if pos1 > 0 and pos2 > 1 and pos2 > pos1 thentitle = Mid( strContent, pos1, pos2 - pos1+1 )strContent=Replace(strContent,title,"")pos2=1end if%><%loop%>

慕尼黑8549860

Mid函数是从第一个字符开始取值的如果 pos1 = InStr(1, strContent, "<", vbTextCompare) 这句话得到的POS1小于1的话,程序Mid函数当然就会报错了
随时随地看视频慕课网APP
我要回答