在vba中没有id刮html

我试图从网站http://us.spindices.com/indices/equity/sp-oil-gas-exploration-production-select-industry-获取月初至今的返回值,使用VBA 索引到Excel电子表格中。 问题是在页面的代码中没有“id =”,我知道这会使这个过程简单得多。 还有哪个时间段(年初至今或月初至今)是可见的,但我现在很高兴能够抓取MTD值。

这是我的代码:

Sub Get_Change() 'attempting to scrape Barclay's website Dim appIE As Object Dim MyVar As String Set appIE = CreateObject("internetexplorer.application") With appIE .Navigate "http://us.spindices.com/indices/equity/sp-oil-gas-exploration-production-select-industry-index" .Visible = True End With Do While appIE.Busy DoEvents Range("A1").Value = "Working..." Loop Set TDelements = appIE.document.getElementsbyClassName("performance-chart-table") For Each TDelement In TDelements If TDelement.class = "change" Then MyVar = TDelement.class.innerText("Value") End If Next Range("A1").Value = MyVar appIE.Quit Set appIE = Nothing End Sub 

如果我可以将“MyVar”variables设置为当前的MTD或YTD值,我会完成,但由于没有这些值的唯一标识符,所以我很难。 有任何想法吗?

我最近看了一些CSS培训video,我可以告诉你CSSselect器语法是强大的,我会推荐它。 这与javascript / web开发人员在使用JQuery时用于select元素的语法相同。

我想你应该尝试使用

document.queryselectorall

或者在你的情况下,因为你已经钻入到文档,以获得“性能图表”调用queryselectorallclosures该variables, TDelements

文档在http://www.w3schools.com/jsref/met_document_queryselectorall.asp

你可以在http://www.w3schools.com/cssref/css_selectors.aspfind一个CSSselect器string的参数。

我已经为你做了…

 Sub Get_Change() '* Tools-References Microsoft HTML Object Library 'attempting to scrape Barclay's website Dim appIE As Object Dim MyVar As String Set appIE = CreateObject("internetexplorer.application") With appIE .Navigate "http://us.spindices.com/indices/equity/sp-oil-gas-exploration-production-select-industry-index" .Visible = True End With Do While appIE.Busy DoEvents Range("A1").Value = "Working..." Loop Dim htmlDoc As MSHTML.HTMLDocument Set htmlDoc = appIE.document Dim TDelements2 As MSHTML.IHTMLElementCollection Set TDelements2 = htmlDoc.getElementsByClassName("performance-chart-table") While TDelements2.Length < 1 DoEvents Application.Wait (Now() + TimeSerial(0, 0, 3)) Set TDelements2 = htmlDoc.getElementsByClassName("performance-chart-table") Wend Dim oHTMLTablePerformanceChartTable As MSHTML.HTMLTable Set oHTMLTablePerformanceChartTable = TDelements2.Item(0) Dim objChangeCollection As MSHTML.IHTMLDOMChildrenCollection Set objChangeCollection = oHTMLTablePerformanceChartTable.querySelectorAll(".change") 'Debug.Assert objChangeCollection.Length = 2 Dim objChange2 As Object Set objChange2 = objChangeCollection.Item(1) MyVar = objChange2.innerText 'Set TDelements = appIE.document.getElementsByClassName("performance-chart-table") ' 'For Each TDelement In TDelements ' TDelements.querySelectorAll (".change") ' If TDelement.class = "change" Then ' MyVar = TDelement.class.innerText("Value") ' ' End If 'Next Range("A1").Value = MyVar appIE.Quit Set appIE = Nothing End Sub