我已经成功使用以下代码从Marketwatch.com提取了共同基金的绩效数据:
Dim A As Long
Dim B As Long
Dim C As Long
Dim Z As Long
For Z = 1 To 35
Range("A1").Select
ActiveCell.Offset((37 + (Z * 10)), 0).Select
If ActiveCell.Value = "" Then
Exit For
Else
End If
Dim oHTML As Object
Dim oTable As Object
Dim x As Long
Dim Y As Long
Dim vData As Variant
Set oHTML = CreateObject("HTMLFile")
With CreateObject("WinHTTP.WinHTTPRequest.5.1")
.Open "GET", "http://www.marketwatch.com/investing/fund/vfinx", False
.send
oHTML.body.innerhtml = .responsetext
End With
For Each oTable In oHTML.Getelementsbytagname("table")
If oTable.classname = "fundstable" Then
ReDim vData(1 To oTable.Rows.Length, 1 To oTable.Rows(1).Cells.Length)
For x = 1 To UBound(vData)
For Y = 1 To UBound(vData, 2)
vData(x, Y) = oTable.Rows(x - 1).Cells(Y - 1).innertext
Next Y
Next x
With ActiveCell.Offset(1, 0)
.Resize(UBound(vData), UBound(vData, 2)).Value = vData
End With
Exit For
End If
Next oTable
Next Z
不幸的是,Marketwatch已添加了一个验证码,以阻止机器人(即我)抓取其数据。我对此一无所知,所以我想尝试另一个网站。
我看着晨星:http://performance.morningstar.com/fund/performance-return.action?t=VFINX®ion=usa&culture=en_US
看来我在该页面上想要的表应该是:“ table.r_table3 width955px print97”或仅仅是“ r_table3 width955px print97”,但似乎没有一个适合我。
有什么想法吗?
谢谢!
答案 0 :(得分:1)
数据由javascript加载,由于无法运行脚本来加载内容,因此无法通过XMLHTTP请求获得。
例如,您可以将第二个链接用于IE,并引入等待以确保加载了信息。我展示了在索引1处获得具有该类名称的表。您可以在此处更改索引:
ele.item(1).outerHTML
因此,对于下一张表,请使用clipboard.SetText ele.item(2).outerHTML
。
您还可以循环.Length
中的ele
来获取每个表,但是请确保在粘贴时写出到不同的单元格:
Dim i As Long
For i = 0 To ele.Length-1
clipboard.SetText ele.item(i).outerHTML
'Etc
Next
VBA:
Option Explicit
Public Sub GetInfo()
Dim IE As New InternetExplorer, clipboard As Object
Dim ele As Object, ws As Worksheet, t As Date, tableCount As Long
Const MAX_WAIT_SEC As Long = 5
Set ws = ThisWorkbook.Worksheets("Sheet1")
Set clipboard = GetObject("New:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
With IE
.Visible = True
.navigate "http://performance.morningstar.com/fund/performance-return.action?t=VFINX®ion=usa&culture=en_US"
While .Busy Or .readyState < 4: DoEvents: Wend
With .document
t = Timer
Do
DoEvents
On Error Resume Next
Set ele = .querySelectorAll(".r_table3.print97")
tableCount = ele.Length
On Error GoTo 0
If Timer - t > MAX_WAIT_SEC Then Exit Do
Loop While tableCount < 3
If Not ele Is Nothing Then
clipboard.SetText ele.item(1).outerHTML
clipboard.PutInClipboard
ws.Cells(1, 1).PasteSpecial
End If
End With
.Quit
End With
End Sub