[英]How do I get the data of a website as shown in INSPECT ELEMENT and not in VIEW PAGE SOURCE?
[英]In VBA, How can we get data shown up with "Inspect Element", but not with "View Page Source"?
我正在嘗試抓取包含多個選項卡的 web 頁面。 我想獲取單擊“按季度”選項卡時顯示的季度數據,但我的代碼不斷返回單擊“按年”選項卡時顯示的年度數據。 問題是兩種類型的數據都在同一個 URL 上,右擊“Inspect Element”時,它們的 ID 也相同; 您無法區分季度數據元素 ID 和年度數據元素 ID。 “Inspect Element”顯示季度和年度數據,但“View Page Source”僅顯示年度數據。 誰能告訴我如何獲得季度數據嗎? 非常感謝。
Sub Getquarterdata()
Dim html As HTMLDocument
Set html = New HTMLDocument
URL = "https://s.cafef.vn/hose/VCB-ngan-hang-thuong-mai-co-phan-ngoai-thuong-viet-nam.chn"
With CreateObject("MSXML2.XMLHTTP")
.Open "GET", URL, False
.SetRequestHeader "User-Agent", "Mozilla/5.0"
.send
html.body.innerHTML = .responseText
End With
' By "Inspect Element" pointing at Quarterly Data, I counted "td" and came with these lines of code, but they print yearly data.
Debug.Print html.getElementById("divHoSoCongTyAjax").getElementsByTagName("td")(23).innerText '=> Print 9,091,070,000 (Year 2017 data)
Debug.Print html.getElementById("divHoSoCongTyAjax").getElementsByTagName("td")(24).innerText '=> Print 14,605,578,000 (Year 2018 data)
Debug.Print html.getElementById("divHoSoCongTyAjax").getElementsByTagName("td")(25).innerText '=> Print 18,510,898,000 (Year 2019 data)
Debug.Print html.getElementById("divHoSoCongTyAjax").getElementsByTagName("td")(26).innerText '=> Print 18,451,311,000 (Year 2020 data)
' The thing is that Quarterly Data shows up only with "Inspect Element", but not with "View Page Source"
Set html = Nothing
結束子
鏈接
URL: https://s.cafef.vn/hose/VCB-ngan-hang-thuong-mai-co-phan-ngoai-thuong-viet-nam.chn
單擊按季度選項卡時顯示的季度數據https://drive.google.com/file/d/1oRtrBZxAoKgdE7gMSBsmkpSX_Ljv1c7L/view?usp=sharing
單擊按年選項卡時顯示的年度數據https://drive.google.com/file/d/1-tI5TU7IMOXFIhsfH8tGvsCRoB0O7Xl1/view?usp=sharing
檢查季度數據: https://drive.google.com/file/d/1Xc5hRPTBIKFu7hQoLh4mStp92CxipNpU/view?usp=sharing
檢查年度數據: https://drive.google.com/file/d/1LedAF3gvAYSIOKOKfZURR9A2rhK0SNgB/view?usp=sharing
給出的線索之一是在 class 中,您會看到它Ajax
。 這是動態添加的內容。 如果您使用開發工具 (F12) 的 .network 選項卡,並手動 select 季度選項卡,您將看到以下請求端點,它提供您所需要的數據:
https://s.cafef.vn/Ajax/Bank/BHoSoCongTy.aspx?symbol=VCB&Type=1&PageIndex=0&PageSize=4&donvi=1
Option Explicit
Public Sub GetQuarterlyTable()
'required VBE (Alt+F11) > Tools > References > Microsoft HTML Object Library ; Microsoft XML, v6 (your version may vary)
Dim hTable As MSHTML.HTMLTable
Dim xhr As MSXML2.XMLHTTP60, html As MSHTML.HTMLDocument
Set xhr = New MSXML2.XMLHTTP60
Set html = New MSHTML.HTMLDocument
With xhr
.Open "GET", "https://s.cafef.vn/Ajax/Bank/BHoSoCongTy.aspx?symbol=VCB&Type=1&PageIndex=0&PageSize=4&donvi=1", False
.send
html.body.innerHTML = .responseText
End With
Set hTable = html.querySelector(".tab1child_content")
'Do something with table
Stop
End Sub
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.