北京 | 上海 | 天津 | 重庆 | 广州 | 深圳 | 珠海 | 汕头 | 佛山 | 中山 | 东莞 | 南京 | 苏州 | 无锡 | 常州 | 南通 | 扬州 | 徐州 | 杭州 | 温州 | 宁波 | 台州 | 福州 | 厦门 | 泉州 | 龙岩 | 合肥 | 芜湖 | 成都 | 遂宁 | 长沙 | 株洲 | 湘潭 | 武汉 | 南昌 | 济南 | 青岛 | 烟台 | 潍坊 | 淄博 | 济宁 | 太原 | 郑州 | 石家庄 | 保定 | 唐山 | 西安 | 大连 | 沈阳 | 长春 | 昆明 | 兰州 | 哈尔滨 | 佳木斯 | 南宁 | 桂林 | 海口 | 贵阳 | 西宁 | 乌鲁木齐 | 包头 |
参考了本论坛上的一个贴子,能够在登陆内网网页后,获取到内网网页的数据,网页翻页后,也能够从新获取到新的数据,如是几十页,操作起来很不是滋味.现在向各位老师请求解决下面一段代码语句中,怎样取获取网页数据表头及一次性获取多页数据,请各位老师给予赐教
On Error Resume Next
If Dir(CurrentProject.Path & "\数据采集.xls") <> "" Then '寻找指定文件,如果该文件存在则执行下面删除文件命令
Kill CurrentProject.Path & "\数据采集.xls" '删除指定的文件
End If
MaxP = 1
If MaxP > 0 Then
Dim Tables As IHTMLElementCollection
Set Tables = W1.Document.getElementsByTagName("Table")
Dim Table1 As HTMLTable
Dim i As Integer
Dim J As Integer
Dim text1 As String
Dim TX As String
Dim N As Integer '使用此参数用于确定是否找到了数据行
Set newBook = Workbooks.Add '新增一个工作簿实例,以便后面代码写入数据
txtData = Me.W1.Object.Document.body.innerText '获取页面中的去除html的数据
If Left(txtData, 4) = " 已取消" Then
MsgBox "目标网页没有正常打开,请检查网络连接。", vbOKOnly, "系统提示" '消息提示
Exit Sub
End If
For Each Table1 In Tables
M = 0 '行数置0,否则第二次运行在excel表中会增加上次运行总行数的空白行
Dim Row As HTMLTableRow, Cell As HTMLTableCell
N = 0
For i = 0 To Table1.rows.length - 1 ' 逐行处理
Set Row = Table1.rows(i)
J = 0
TX = Trim(Row.cells(0).innerText)
If Left(Trim(Row.cells(0).innerText), 4) = "物料编码" Then N = 1 ' 找到需要的行,不要表头
If N = 1 And Left(Trim(Row.cells(0).innerText), 5) <> ">> 首页" Then
If i = 1 And M = 0 Then M = M + 1
If i > 1 Then M = M + 1
For Each Cell In Row.cells ' 逐列处理
If i = 1 And M = 1 Then newBook.worksheets(1).cells(M, 1 + J) = Trim(Row.cells(J).innerText)
If i > 1 Then newBook.worksheets(1).cells(M, 1 + J) = Trim(Row.cells(J).innerText)
If i > 1 Then text1 = text1 & Trim(Row.cells(J).innerText) & ","
J = J + 1
Next
If i = 1 And M = 1 Then newBook.worksheets(1).cells(M, 1 + J) = Date
If Nz(text1) <> "" Then
text1 = text1 & Format(CDate(Date) - PP, "yyyy-m-d") & vbCrLf ' 一行处理完毕后,去除行尾的逗号并加上回车
If i > 1 Then newBook.worksheets(1).cells(M, 1 + J) = Format(CDate(Date) - PP, "yyyy-m-d")
End If
End If
Next
Next
End If
newBook.SaveAs Filename:=CurrentProject.Path & "\数据采集.XLS" '创建新的数据文件,并保存在同一文件夹下
newBook.Close '关闭文件
If MsgBox("数据己传输完毕,文件己保存到:" & CurrentProject.Path & "\数据采集.XLS" & vbCrLf & "现在查看和整理数据吗?", vbYesNo + vbDefaultButton1, "系统提示") = vbYes Then
Dim newXls As Excel.Application
Set newXls = CreateObject("Excel.Application")
newXls.Visible = True
newXls.UserControl = True
newXls.Workbooks.Open CurrentProject.Path & "\数据采集.XLS" '打开指定的文件
Else
M = 0 '还原记数
End If