VBA网络垃圾不收集垃圾;拿起框架/桌子?
我试着问这个问题。没有得到很多答案。无法在我的工作计算机上安装东西 希望使用下面的代码将晨星页面刮到Excel中。问题是,它不会反馈任何真实的元素/数据。实际上,我只想从我放在我的_页面的链接中得到股息和上限收益分配表 这通常是最简单的方法,但整个页面的刮取方式和Excel-->数据-->从Web上无法工作 我以前尝试过使用标记名和类来获取元素,但在这种情况下无法做到。这可能是一种方法。。。再一次,我们只需要股息和上限收益分配表。未通过Debug.print在中看到任何结果 下面的工作代码,只需解析为excel即可。更新尝试如下:VBA网络垃圾不收集垃圾;拿起框架/桌子?,vba,excel,tags,web-scraping,js,Vba,Excel,Tags,Web Scraping,Js,我试着问这个问题。没有得到很多答案。无法在我的工作计算机上安装东西 希望使用下面的代码将晨星页面刮到Excel中。问题是,它不会反馈任何真实的元素/数据。实际上,我只想从我放在我的_页面的链接中得到股息和上限收益分配表 这通常是最简单的方法,但整个页面的刮取方式和Excel-->数据-->从Web上无法工作 我以前尝试过使用标记名和类来获取元素,但在这种情况下无法做到。这可能是一种方法。。。再一次,我们只需要股息和上限收益分配表。未通过Debug.print在中看到任何结果 下面的工作代码,只需
Sub Macro1()
Dim IE As New InternetExplorer
IE.Visible = True
IE.navigate "http://quotes.morningstar.com/fund/fundquote/f?&t=ANNPX&culture=en_us&platform=RET&viewId1=2046632524&viewId2=3141452350&viewId3=3475652630"
Do
DoEvents
Loop Until IE.readyState = READYSTATE_COMPLETE
Dim doc As HTMLDocument
Set doc = IE.document
'For Each Table In doc.getElementsByClassName("gr_table_b1")
'For Each td In Table.getElementsByTagName("tr")
On Error Resume Next
For Each td In doc.getElementsByClassName("gr_table_row4")
Debug.Print td.Cells(5).innerText
'Debug.Print td.Cells(1).innerText
Next td
'Next Table
'IE.Quit
'Application.EnableEvents = True
End Sub
所讨论的内容包含在iframe中。您可以通过右键单击站点的该部分并选择
Inspect element
来查看这一点。查找树,您将看到一个iframe标记,其中包含数据的url。您应该尝试查找该元素,并提取该url(由生成),然后打开该页面。无需担心框架。您只需要表id
网页视图:
Option Explicit
Public Sub GetDivAndCapTable()
Dim ie As New InternetExplorer, hTable As HTMLTable
Const URL = "http://quotes.morningstar.com/fund/fundquote/f?&t=ANNPX&culture=en_us&platform=RET&viewId1=2046632524&viewId2=3141452350&viewId3=3475652630"
Application.ScreenUpdating = False
With ie
.Visible = True
.navigate URL
While .Busy Or .READYSTATE < 4: DoEvents: Wend
Set hTable = .document.getElementById("DividendAndCaptical")
WriteTable hTable, 1
Application.ScreenUpdating = True
.Quit
End With
End Sub
Public Sub WriteTable(ByVal hTable As HTMLTable, Optional ByVal startRow As Long = 1, Optional ByVal ws As Worksheet)
If ws Is Nothing Then Set ws = ActiveSheet
Dim tSection As Object, tRow As Object, tCell As Object, tr As Object, td As Object, R As Long, C As Long, tBody As Object
R = startRow
With ws
Dim headers As Object, header As Object, columnCounter As Long
Set headers = hTable.getElementsByTagName("th")
For Each header In headers
columnCounter = columnCounter + 1
.Cells(startRow, columnCounter) = header.innerText
Next header
startRow = startRow + 1
Set tBody = hTable.getElementsByTagName("tbody")
For Each tSection In tBody 'HTMLTableSection
Set tRow = tSection.getElementsByTagName("tr") 'HTMLTableRow
For Each tr In tRow
Set tCell = tr.getElementsByTagName("td")
C = 1
For Each td In tCell 'DispHTMLElementCollection
.Cells(R, C).Value = td.innerText 'HTMLTableCell
C = C + 1
Next td
R = R + 1
Next tr
Next tSection
End With
End Sub
从代码打印出来:
Option Explicit
Public Sub GetDivAndCapTable()
Dim ie As New InternetExplorer, hTable As HTMLTable
Const URL = "http://quotes.morningstar.com/fund/fundquote/f?&t=ANNPX&culture=en_us&platform=RET&viewId1=2046632524&viewId2=3141452350&viewId3=3475652630"
Application.ScreenUpdating = False
With ie
.Visible = True
.navigate URL
While .Busy Or .READYSTATE < 4: DoEvents: Wend
Set hTable = .document.getElementById("DividendAndCaptical")
WriteTable hTable, 1
Application.ScreenUpdating = True
.Quit
End With
End Sub
Public Sub WriteTable(ByVal hTable As HTMLTable, Optional ByVal startRow As Long = 1, Optional ByVal ws As Worksheet)
If ws Is Nothing Then Set ws = ActiveSheet
Dim tSection As Object, tRow As Object, tCell As Object, tr As Object, td As Object, R As Long, C As Long, tBody As Object
R = startRow
With ws
Dim headers As Object, header As Object, columnCounter As Long
Set headers = hTable.getElementsByTagName("th")
For Each header In headers
columnCounter = columnCounter + 1
.Cells(startRow, columnCounter) = header.innerText
Next header
startRow = startRow + 1
Set tBody = hTable.getElementsByTagName("tbody")
For Each tSection In tBody 'HTMLTableSection
Set tRow = tSection.getElementsByTagName("tr") 'HTMLTableRow
For Each tr In tRow
Set tCell = tr.getElementsByTagName("td")
C = 1
For Each td In tCell 'DispHTMLElementCollection
.Cells(R, C).Value = td.innerText 'HTMLTableCell
C = C + 1
Next td
R = R + 1
Next tr
Next tSection
End With
End Sub
VBA:
Option Explicit
Public Sub GetDivAndCapTable()
Dim ie As New InternetExplorer, hTable As HTMLTable
Const URL = "http://quotes.morningstar.com/fund/fundquote/f?&t=ANNPX&culture=en_us&platform=RET&viewId1=2046632524&viewId2=3141452350&viewId3=3475652630"
Application.ScreenUpdating = False
With ie
.Visible = True
.navigate URL
While .Busy Or .READYSTATE < 4: DoEvents: Wend
Set hTable = .document.getElementById("DividendAndCaptical")
WriteTable hTable, 1
Application.ScreenUpdating = True
.Quit
End With
End Sub
Public Sub WriteTable(ByVal hTable As HTMLTable, Optional ByVal startRow As Long = 1, Optional ByVal ws As Worksheet)
If ws Is Nothing Then Set ws = ActiveSheet
Dim tSection As Object, tRow As Object, tCell As Object, tr As Object, td As Object, R As Long, C As Long, tBody As Object
R = startRow
With ws
Dim headers As Object, header As Object, columnCounter As Long
Set headers = hTable.getElementsByTagName("th")
For Each header In headers
columnCounter = columnCounter + 1
.Cells(startRow, columnCounter) = header.innerText
Next header
startRow = startRow + 1
Set tBody = hTable.getElementsByTagName("tbody")
For Each tSection In tBody 'HTMLTableSection
Set tRow = tSection.getElementsByTagName("tr") 'HTMLTableRow
For Each tr In tRow
Set tCell = tr.getElementsByTagName("td")
C = 1
For Each td In tCell 'DispHTMLElementCollection
.Cells(R, C).Value = td.innerText 'HTMLTableCell
C = C + 1
Next td
R = R + 1
Next tr
Next tSection
End With
End Sub
选项显式
公共子GetDivAndCapTable()
Dim ie作为新的InternetExplorer,hTable作为HTMLTable
常量URL=”http://quotes.morningstar.com/fund/fundquote/f?&t=ANNPX&culture=en_us&platform=RET&viewId1=2046632524&viewId2=3141452350&viewId3=3475652630"
Application.ScreenUpdating=False
与ie
.Visible=True
.浏览网址
当.Busy或.READYSTATE<4:DoEvents:Wend时
Set hTable=.document.getElementById(“dividendCapital”)
可写的hTable,1
Application.ScreenUpdating=True
退出
以
端接头
Public Sub-WriteTable(ByVal-httable作为HTMLTable,可选ByVal-startRow作为Long=1,可选ByVal-ws作为工作表)
如果ws为Nothing,则设置ws=ActiveSheet
Dim tSection作为对象,tRow作为对象,tCell作为对象,tr作为对象,td作为对象,R作为长,C作为长,tBody作为对象
R=startRow
与ws
将标题设置为对象、标题设置为对象、列计数器设置为长
Set headers=hTable.getElementsByTagName(“th”)
对于标题中的每个标题
columnCounter=columnCounter+1
.Cells(startRow,columnCounter)=header.innerText
下一标题
startRow=startRow+1
Set tBody=hTable.getElementsByTagName(“tBody”)
对于tBody'HTMLTableSection中的每个t节
设置tRow=tSection.getElementsByTagName(“tr”)HTMLTableRow
对于每个tr-In-tRow
设置tCell=tr.getElementsByTagName(“td”)
C=1
对于tCell的DispHTMLElementCollection中的每个td
.Cells(R,C).Value=td.innerText'HTMLTableCell
C=C+1
下一个td
R=R+1
下一个tr
下一节
以
端接头
我当前的环境有一个旧版本的IE,它不能正确呈现页面,因此我无法构建一些东西来实际执行此操作。iframe就是这个。我的调试现在产生了一些东西(它只是说“YTD”)。我想如果我能把这些标签弄对的话,我现在就可以做生意了。我得到了一个不同的网址在我这边。要由生成的ViewID接缝。我不确定如果你硬编码这些值会发生什么。谢谢你花时间研究这个问题。介意粘贴您看到的URL吗?@pjhollow