【发布时间】:2021-06-30 14:37:55
【问题描述】:
我需要通过单击下面网页中的页码来抓取每个页面上的日期。
我提到了与我的 html 网页相似的示例网站。
示例网页是这个Webpage。
我的代码如下:
Sub Test()
Dim IE As Object
Dim i As Long, strText As String
Dim y As Long, z As Long, wb As Excel.Workbook, ws As Excel.Worksheet
Dim myBtn As Object
Dim Table As Object, tbody As Object, datarow As Object, thlist As Object, trlist As Object
Set wb = Excel.ActiveWorkbook
Set ws = wb.ActiveSheet
Sheets("Data").Select
Set IE = CreateObject("InternetExplorer.Application")
my_url = webpage.com
With IE
.Visible = True
.navigate my_url
Do Until Not IE.Busy And IE.readyState = 4
DoEvents
Loop
End With
Set doc = IE.document
y = 1
z = 1
Application.Wait Now + TimeValue("00:00:02")
Set tbody = IE.document.getElementsByTagName("table")(0).getElementsByTagName("tbody")(0)
Set thlist = tbody.getElementsByTagName("tr")(0).getElementsByTagName("th")
Dim ii As Integer
For ii = 0 To thlist.Length - 1
ws.Cells(z, y).Value = thlist(ii).innerText
y = y + 1
Next ii
Set datarow = tbody.getElementsByTagName("tr")
y = 1
z = 2
Dim jj As Integer
Dim datarowtdlist As Object
For jj = 1 To datarow.Length - 4
Set datarowtdlist = datarow(jj).getElementsByTagName("td")
Dim hh As Integer, x As Integer
x = y
For hh = 0 To datarowtdlist.Length - 1
ws.Cells(z, x).Value = datarowtdlist(hh).innerText
x = x + 1
Next hh
z = z + 1
Next jj
Set IE = Nothing
End Sub
如果我的问题不清楚,我很乐意提供帮助。
感谢您的支持。
【问题讨论】:
-
@QHarr,我们无需输入任何内容。只需进入网站并提取表格..
-
需要根据页码提取表格中的内容。 (页码位于屏幕右侧的底部)。无需输入任何搜索条件。
-
ie.document.parentwindow.execScript "javascript:__doPostBack('sb$grd','Page$10');"? -
QHarr,我应该在上面的代码中在哪里包含这一行以及如何单击页码直到最后一页。
-
能否更新代码让我理解..
标签: html excel vba web-scraping