再次执行2hand-cj.asp ,效果如下图2,只保留了表格,大功告成!
七、替换网页中的数据
检查一下抓取的表格中每个帖子网址,其格式均为InformationDisplay.php?id=,这样的网址是不正确的!应该替换成http://market.ah163.net/city/InformationDisplay_enter.php?id=才行,所以我们在clsThief类中再增加一个替换程序change(oldStr,str),用于替换网址,其中参数oldStr,str分别是旧字符串,新字符串。
public sub change(oldStr,str) '对偷到的内容中的个别字符串用新值更换/方法
if isGet_= false then call steal()
value_=replace(value_ , oldStr,str)
end sub
同时在2hand-cj.asp中也增加如下调用:
<%
myThief.change "
执行2hand-cj.asp,表格中帖子的网址InformationDisplay.php?id=都会替换成http://market.ah163.net/city/InformationDisplay_enter.php?id=
这样帖子的网址都正确生成了。
八、截取帖子标题、网址等
现在我们需要截取每个帖子的标题、网址、方式、价格、时间(如上图2)这些数据,然后将之写入库中,为此,再写一个GetKey函数,负责截取这些数据,从Start开始截取,到Last截取结束
Function GetKey(HTML,Start,Last)
filearray=split(HTML,Start)
filearray2=split(filearray(1),Last)
GetKey=filearray2(0)
End Function
在抓到的网页代码中(下图3)我们发现,每个帖子的标题都位于和
之间,所以按照如下格式调用GetKey截取帖子的标题:
tittle=GetKey(HTML,"","
"),其他数据的截取如法炮制,先确定截取的起始和结束标志,然后调用GetKey截取。
因此在2hand-cj.asp中增加如下语句:
'-----截取帖子标题
tittle=GetKey(HTML,"","
")
tittle=mid(tittle,6) '去掉头部前6个非显示字符
'-----截取帖子网址
url=GetKey(HTML,"
")
url=TRIM(url) '去掉空格
'-----得到大类别和小类别
CateIDText=GetKey(HTML,"[","]") '截取类别数据
CateIDText=TRIM(CateIDText)
select case CateIDText
case "交通" '如果类别数据=交通
CateID=8 ' 大类别CateID就等于8
SubCateID=1 ' 小类别SubCateID就等于1
case "游戏"
CateID=1
SubCateID=26
case "电脑"
CateID=1
SubCateID=1
case "房产"
CateID=6
SubCateID=1
case "通讯"
CateID=2
SubCateID=1
case "宠物"
CateID=31
SubCateID=221
case "求职"
CateID=37
SubCateID=230
case "影音"
CateID=4
SubCateID=1
case "家用"
CateID=5
case "书籍"
SubCateID=1
CateID=17
case "其它"
CateID=0
SubCateID=1
end select
'-----取得方式
fangshi=GetKey(HTML,"
| ","
")
fangshi=TRIM(right(fangshi,4))
select case fangshi
case "求购"
SoftType="买进"
case "出售"
SoftType="卖出"
end select
if instr(fangshi,""">")>0 then fangshi="其他" '如果fangshi含有字符"> 则fangshi="其他"
'-----取得价格
jiage=GetKey(HTML,"
| ","
")
jiage=TRIM(mid(jiage,44))
'-----取得帖子发布日期
DayDate=GetKey(HTML,"
| ","
")
DayDate=right(DayDate,10)
'-----显示得到的帖子数据
Response.write tittle
Response.write url
Response.write fangshi
Response.write jiage
Response.write DayDate
九、帖子数据入库
最后要把帖子数据tittle、url、fangshi、jiage、DayDate写入#2hand.mdb库中,为防止帖子重复入库,需要写个 testsj函数来判断某帖子是否已入库了,假如某帖子URL在库中找不到,则将该帖入库,否则就不予入库,代码如下:
'检测库中是否有某帖子的URL
Function testsj(titURL)
sql="select * from SoftDown_SoftInfo where url like '%"&titURL&"%' "
set rs=server.createobject("adodb.recordset")
rs.open sql,conn,1,1
if rs.bof and rs.eof then
testsj=True
ErrMsg=ErrMsg & "
你要找的帖子不存在,或者已经被管理员删除!
"
else
testsj=false '库中无该帖子的URL
end if
rs.close
set rs=nothing
End Function
接下来打开数据库语句如下:
db="#2hand.mdb"
Set conn = Server.CreateObject("ADODB.Connection")
connstr="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath(db)
conn.Open connstr
'-----判断帖子是否已经入库?
FoundErr=False
FoundErr=testsj(url)
'-----帖子数据写入库中
if FoundErr=True then
set rs=server.createobject("adodb.recordset")
sql="select * from SoftDown_SoftInfo where (SoftID is null)"
rs.open sql,conn,1,3
if rs.bof and rs.eof then
ErrMsg=ErrMsg & "
你要找的帖子不存在,或者已经被管理员删除!
"
else
ArticleTitle=rs("SoftName")
end if
rs.addnew
rs("SoftName")=tittle
rs("url")=url
rs("CateID")=CateID '所属大类
rs("SubCateID")=SubCateID '所属小类
rs("SoftType")=fangshi '出售\买进\出租\求租等方式
rs("SoftSize")=jiage '价格
rs("hfsj")=DayDate '发布时间
rs.update
rs.close
set rs=nothing
Response.write " 该帖入库成功
"
end if
|