简体   繁体   中英

VBA XMLHTTP request doesn't capture dynamic HTML response

I am trying to get a specific dynamic figure from a webpage to excel, I managed to gather all the website get response into a "all" variable which I am supposed to parse to extract my numbers, except for when I check the string variable I can see everything but the required dynamic figure: .) "the attached phot shows the dynamic figure at the very instant was 2,19", any ideas why I am capturing every thing, would be much appreciated, Thanks in advance

My thoughts: 1.I am guessing is the figures are injected by JavaScript or a server side that might be executing after my XMLHTTP request is processed maybe! if this is the case or else I need your expertise

  1. the website doesn't response unless it sees a specific Html request header, so I might need to mimic the headers of Chrome, I don't know how they look like?

Please see below my code and a screenshot for the figure I would like to capture

'Tools>refrences>microsoft xml v3 must be refrenced
Public Function GetWebSource(ByRef URL As String) As String
    Dim xml As IXMLHTTPRequest
    On Error Resume Next
    Set xml = CreateObject("Microsoft.XMLHTTP")
    With xml
        .Open "GET", URL, False
        .send
        GetWebSource = .responseText
    End With
    Set xml = Nothing
End Function

Sub ADAD()
Dim all As Variant
Dim objHTTP As Object
Dim URL As String

Set objHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
all = GetWebSource("https://www.tradingview.com/symbols/CRYPTOCAP-ADA.D/")


pos = InStr(all, "tv-symbol-price-quote__value js-symbol-last")
testString = Mid(all, pos, 200)
'I am supposed to see the dynamic figure within the TAG but it is not showing!!
Debug.Print testString

End Sub

HTML for Dynamic Required values

@Tim Williams This is a code using selenium (But it seems doesn't do the trick of getting the value)

PhantomJS Selenium VBA

Sub Test()
    Dim bot As Selenium.PhantomJSDriver
    Set bot = New Selenium.PhantomJSDriver
    With bot
        .Get "https://www.tradingview.com/symbols/CRYPTOCAP-ADA.D/"
        .Wait 2000
        Debug.Print .FindElementByXPath("//div[@class='tv-symbol-price-quote__value js-symbol-last']").Attribute("outerHTML")
    End With
End Sub

Chrome VBA Selenium

It seems using PhantomJS doesn't work properly, so here's a Chrome version of selenium in VBA

Private bot As Selenium.ChromeDriver

Sub Test()
    Set bot = New Selenium.ChromeDriver
    With bot
        .Start
        .Get "https://www.tradingview.com/symbols/CRYPTOCAP-ADA.D/"
        Debug.Print .FindElementByXPath("//div[@class='tv-symbol-price-quote__value js-symbol-last']").Text 'Attribute("outerHTML")
        .Quit
    End With
End Sub

Python Solution

And this is the working python code that my tutor @QHarr provided in comments

from selenium import webdriver

d = webdriver.Chrome("D:/Webdrivers/chromedriver.exe")
d.get('https://www.tradingview.com/symbols/CRYPTOCAP-ADA.D/')
d.find_element_by_css_selector('.tv-symbol-price-quote__value.js-symbol-last').text

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM