簡體   English   中英

VBA XMLHTTP 請求未捕獲動態 HTML 響應

[英]VBA XMLHTTP request doesn't capture dynamic HTML response

我正在嘗試從網頁獲取特定的動態圖形到 excel,我設法將所有網站獲取響應收集到一個“全部”變量中,我應該解析該變量以提取我的數字,除非我檢查字符串變量 I可以看到除了所需的動態圖之外的所有內容:。)“所附照片顯示了瞬間的動態圖是 2,19”,任何我捕捉每件事的想法,將不勝感激,在此先感謝

我的想法: 1.我猜這些數字是由 JavaScript 或在我的 XMLHTTP 請求處理后可能正在執行的服務器端注入的! 如果是這種情況,否則我需要你的專業知識

  1. 該網站沒有響應,除非它看到特定的 Html 請求 header,所以我可能需要模仿 Chrome 的標頭,我不知道它們長什么樣?

請在下面查看我的代碼和我想要捕獲的圖的屏幕截圖

'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 用於動態所需值

@Tim Williams 這是一個使用 selenium 的代碼(但它似乎沒有獲得價值的訣竅)

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

鍍鉻 VBA Selenium

似乎使用PhantomJS不能正常工作,所以這里是 VBA 中 selenium 的 Chrome 版本

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 解決方案

這是我的導師@QHarr 在評論中提供的工作 python 代碼

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM