我想在此页面的技术摘要小部件中获得买入,卖出,中性的投票数量:https://in.tradingview.com/symbols/NSE-TCS/
元素是这些
<span class="tv-widget-technicals__counter-number redColor">2</span>
<span class="tv-widget-technicals__counter-number neutralColor">10</span>
<span class="tv-widget-technicals__counter-number brandColor">8</span>
我尝试了不同的东西,但我不知道如何访问这些元素。我甚至尝试循环遍历所有span元素,但没有显示在列表中。
我已经添加了我到目前为止所尝试的内容,请让我知道如何解决这个问题。
Sub Test_Macro()
Dim i As Long, j As Long, fD As Long
Dim symBol As String, urL As String, hdoc
Dim oHtml As HTMLDocument
Dim oElement As Object
Dim dados
With DATA
fD = .Range("A" & .Rows.Count).End(xlUp).Row
Set oHtml = New HTMLDocument
With CreateObject("WINHTTP.WinHTTPRequest.5.1")
For i = 2 To fD
symBol = Trim(Replace(Replace(DATA.Range("A" & i).Value, "-", "_"), "&", "_"))
urL = "https://in.tradingview.com/symbols/NSE-" & symBol
.Open "GET", urL, False
.send
oHtml.body.innerHTML = .responseText
Stop
Set dados = oHtml.getElementsByClassName("tv-site-widget ").Item(1).getElementsByTagName("span")
j = 1
For Each oElement In dados
DATA.Range("F" & j) = oElement.innerText
j = j + 1
'Debug.Print oElement.innerHTML
Next oElement
Stop
Next i
End With
End With
End Sub
答案 0 :(得分:3)
尝试以下方式。您所追踪的内容是动态生成的。即使您使用IE
之类的任何浏览器模拟器,也无法获取内容,除非您让浏览器等待一段时间才能加载数字。最重要的是,您无法使用xmlhttp,winhttp或serverxmlhttp请求获取所需的输出,因为他们不处理动态内容。试一试:
Sub TestMacro()
Const URL As String = "https://in.tradingview.com/symbols/NSE-TCS/"
Dim IE As New InternetExplorer, oHtml As HTMLDocument, post As Object, R&
With IE
.Visible = True
.navigate URL
While .Busy Or .readyState < 4: DoEvents: Wend
Set oHtml = .document
End With
Application.Wait Now + TimeValue("00:00:05")
For Each post In oHtml.getElementsByClassName("tv-widget-technicals__counter-wrapper")
With post.getElementsByTagName("span")
R = R + 1: Cells(R, 1) = .Item(0).innerText
End With
Next post
End Sub
此时输出:
2
10
14
参考添加:
Microsoft Internet Controls
Microsoft HTML Object Library