【发布时间】:2018-11-14 21:15:33
【问题描述】:
我已使用以下代码从 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”,但似乎都不适合我。
有什么想法吗?
谢谢!
【问题讨论】:
标签: html excel vba web-scraping