在vba中删除没有id的html
我试图从网站上获取月到日期和年到日期的返回值 使用VBA导入Excel电子表格。问题是页面的代码中没有“id=”,据我所知,这会使这个过程简单得多。还有一个问题是哪个时间段(年初至今还是月至今)是可见的,但我很乐意现在只删除MTD值 这是我的密码:在vba中删除没有id的html,html,excel,vba,web-scraping,Html,Excel,Vba,Web Scraping,我试图从网站上获取月到日期和年到日期的返回值 使用VBA导入Excel电子表格。问题是页面的代码中没有“id=”,据我所知,这会使这个过程简单得多。还有一个问题是哪个时间段(年初至今还是月至今)是可见的,但我很乐意现在只删除MTD值 这是我的密码: Sub Get_Change() 'attempting to scrape Barclay's website Dim appIE As Object Dim MyVar As String Set appIE = CreateObject
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”变量设置为当前的MTD或YTD值,我就完成了,但我有一个困难的时间,因为这两个值都没有唯一的标识符。有什么想法吗?我最近看了一些CSS培训视频,我可以告诉你CSS选择器语法非常强大,我推荐它。这与javascript/web开发人员在使用
JQuery
时选择元素时使用的语法相同
我想你应该试试使用
document.queryselectorall
或者在您的情况下,因为您已经深入到文档中,以获取该变量的“性能图表表”调用queryselectorall
,TDelements
文件在
您可以提供一个CSS选择器字符串作为参数,其语法可以在
我已经为你做了这件事
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
Sub Get_Change()
“*工具引用Microsoft HTML对象库
试图刮取巴克莱的网站
作为对象的模糊应用程序
作为字符串的Dim MyVar
设置appIE=CreateObject(“internetexplorer.application”)
和阿皮
.导航“http://us.spindices.com/indices/equity/sp-oil-gas-exploration-production-select-industry-index"
.Visible=True
以
忙的时候做
多芬特
范围(“A1”).Value=“工作…”
环
将htmlDoc设置为MSHTML.HTMLDocument
设置htmlDoc=appIE.document
将TDelements2设置为MSHTML.IHTMLElementCollection
Set TDelements2=htmlDoc.getElementsByClassName(“性能图表表”)
而t元素2.长度<1
多芬特
Wait(Now()+时间序列(0,0,3))
Set TDelements2=htmlDoc.getElementsByClassName(“性能图表表”)
温德
Dim oHTMLTablePerformanceChartTable为MSHTML.HTMLTable
设置oHTMLTablePerformanceChartTable=tElements2。项(0)
Dim objChangeCollection作为MSHTML.IHTMLDOMChildrenCollection
Set-objChangeCollection=oHTMLTablePerformanceChartTable.querySelectorAll(“.change”)
'Debug.Assert objchangecolection.Length=2
Dim objChange2作为对象
设置objChange2=objChangeCollection.Item(1)
MyVar=objChange2.innerText
'Set-TDelements=appIE.document.getElementsByClassName(“性能图表表”)
'
'对于TDelements中的每个TDelement
'TDelements.querySelectorAll(“.change”)
'如果TDelement.class=“更改”,则
'MyVar=TDelement.class.innerText(“值”)
'
"完"
”“接着呢
范围(“A1”)。值=MyVar
阿皮,退出
设置appIE=Nothing
端接头
我最近观看了一些CSS培训视频,我可以告诉你CSS选择器语法非常强大,我推荐它。这与javascript/web开发人员在使用JQuery
时选择元素时使用的语法相同
我想你应该试试使用
document.queryselectorall
或者在您的情况下,因为您已经深入到文档中,以获取该变量的“性能图表表”调用queryselectorall
,TDelements
文件在
您可以提供一个CSS选择器字符串作为参数,其语法可以在
我已经为你做了这件事
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
Sub Get_Change()
“*工具引用Microsoft HTML对象库
试图刮取巴克莱的网站
作为对象的模糊应用程序
作为字符串的Dim MyVar
设置appIE=CreateObject(“internetexplorer.application”)
和阿皮
.导航“http://us.spindices.com/indices/equity/sp-oil-gas-exploration-production-select-industry-index"
.Visible=True
以
忙的时候做
多芬特
范围(“A1”).Value=“工作…”
环
将htmlDoc设置为MSHTML.HTMLDocument
设置htmlDoc=appIE.document
将TDelements2设置为MSHTML.IHTMLElementCollection
Set TDelements2=htmlDoc.getElementsByClassName(“性能图表表”)
而t元素2.长度<1
多芬特
Wait(Now()+时间序列(0,0,3))
Set TDelements2=htmlDoc.getElementsByClassName(“性能图表表”)
温德
Dim oHTMLTablePerformanceChartTable为MSHTML.HTMLTable
设置oHTMLTablePerformanceChartTable=tElements2。项(0)
Dim objChangeCollection作为MSHTML.IHTMLDOMChildrenCollection
Set-objChangeCollection=oHTMLTablePerformanceChartTable.querySelectorAll(“.change”)
'Debug.Assert objchangecolection.Length=2
Dim objChange2作为对象
设置objChange2=objChangeCollection.Item(1)
MyVar=objChange2.innerText
'Set-TDelements=appIE.document.getElementsByClassName(“性能图表表”)
'
'对于TDelements中的每个TDelement
'TDelements.querySelectorAll(“.change”)
'如果TDelement.class=“更改”,则
'MyVar=TDelement.class.innerText(“值”)
'
"完"
”“接着呢
范围(“A1”)。值=MyVar
阿皮,退出
设置appIE=Nothing
端接头
哇,太棒了,谢谢!当我运行代码时,我得到一个运行时错误;我需要在引用库中选择一个对象才能正确执行吗?第34行-以.querySelectorAll(“.change”)结尾的对象。我已经在谷歌上搜索过了,但我不知道如何修复它…IE11错误91是“对象变量或块变量未设置”的方式OK我在第一行debug.assert上得到了一个424错误“object required”。这有用吗?非常感谢你的帮助。。。我刚开始一份新工作,现在