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

来源:百度知道 编辑:UC知道 时间:2024/06/15 22:38:04
asp中,为什么mid()函数有时出错,有时不会出错?
我想实现抓取某个网页内容的功能,就是想网络蜘蛛那样的
但是,当我把xurl="http://sx.zsedu.net/"换成是xurl="http://www.hao123.com/"时,程序运行正常,但是换成是xurl="http://sx.zsedu.net/"或者时程序运行就出错了,出错是这样说的:

错误类型:
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 res

我觉得我是找到原因了,http://sx.zsedu.net/页面内容里有单独出现的">",如语句:if(dypopLayer.filters.Alpha.opacity>0)中就含有单独的">",故pos1与pos2并不是如你想像中那样完全配套的!

修改也很简单,程序不需要太大变化,只需要把
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
%>

改为:
pos1=1
pos2=1
do while pos1 > 0
%>
<%
pos1 = InStr(1, strContent, "<", vbTextCompare)
pos2 = InStr(pos2+1, strContent, ">", vbTextCompare)
if pos1 > 0 and pos2 > 1 and pos2 > pos1 then
title = Mid( strContent, pos1