问:
==========================
Private Sub Form_Load()
Form1.MousePointer = 11
WebBrowser1.Navigate "http://weather.tq121.com.cn/detail.php?city=北京" ' 起始网址
End Sub
Private Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, URL As Variant)
Dim Tables As IHTMLElementCollection
Set Tables = WebBrowser1.Document.getElementsByTagName("Table")
Dim i As Integer
Dim j As Integer
Dim Table1 As HTMLTable
For Each Table1 In Tables
Next
Dim Row As HTMLTableRow, Cell As HTMLTableCell
j = Table1.rows.Length - 2
For i = 1 To Table1.rows.Length - 1 ' 逐行处理
Set Row = Table1.rows(i)
j = 0
For Each Cell In Row.cells ' 逐列处理
' Row.cells(j).innerText即为当前行及当前列上的单元数据
Text1.Text = Text1.Text + Trim(Row.cells(j).innerText) + ","
j = j + 1
Next
' 一行处理完毕后,去除行尾的逗号并加上回车
Text1.Text = Left(Text1.Text, Len(Text1.Text) - 1) + vbCrLf
Next
End Sub
上面是抓取网页上信息的代码,但是运行到For i = 1 To Table1.rows.Length - 1 ' 逐行处理 这一句时就报“对象变量或with块变量未设置”的错,头都大了。
===========================================
在WebBrowser1下载网页的过程中,有可能多次触发DocumentComplete事件,而此时网页并没有完成,可能还没有HTMLTable对象,当然会出错了,在DocumentComplete事件子程序中加入这样一句试试:
[code=vbscript]
if instr(url,"http://weather.tq121.com.cn/detail.php?city=北京")=0 then exit sub
[/code]
===========================================
Private Sub Form_Load()
Form1.MousePointer = 11
WebBrowser1.Navigate "http://weather.tq121.com.cn/detail.php?city=北京 " ' 起始网址
End Sub
Private Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, URL As Variant)
Dim Tables As IHTMLElementCollection
Set Tables = WebBrowser1.Document.getElementsByTagName( "Table ")
Dim i As Integer
Dim j As Integer
Dim Table1 As HTMLTable For Each Table1 In Tables
Next (这个应该是外层循环,楼主的TABLE1 应该在这层循环内有效,而按你现在的写法,这个循环没有执行任何语句)
Dim Row As HTMLTableRow, Cell As HTMLTableCell
j = Table1.rows.Length - 2 ‘这里已经退出循环,当然是无效了。
For i = 1 To Table1.rows.Length - 1 ' 逐行处理
Set Row = Table1.rows(i)
j = 0
For Each Cell In Row.cells ' 逐列处理
' Row.cells(j).innerText即为当前行及当前列上的单元数据
Text1.Text = Text1.Text + Trim(Row.cells(j).innerText) + ", "
j = j + 1
Next
' 一行处理完毕后,去除行尾的逗号并加上回车
Text1.Text = Left(Text1.Text, Len(Text1.Text) - 1) + vbCrLf
Next