You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

使用Excel VBA通过XMLHttpRequest抓取Stockrow数据失败求助

解决VBA XMLHttpRequest抓取动态页面数据的问题

Hey there! I totally get where you're coming from—you see the full financial table in your browser's dev tools, but your XMLHttpRequest only pulls the basic static HTML. Let's break down why this happens and how to fix it.

Why the table is missing

The page you're targeting uses JavaScript to dynamically load and render the financial data table. When you use XMLHttpRequest, it only fetches the initial, raw HTML sent by the server—it doesn't execute any JavaScript. That's why you're missing the table: it gets built by the stockrow-a35b20c51d525016f7c7.js script after the initial page loads, which XMLHTTP doesn't process.

Solution 1: Use Internet Explorer (easy for beginners)

Since IE acts like a full browser (it executes JavaScript and waits for the page to fully render), it's a straightforward fix for your use case. Here's a sample VBA script:

Sub FetchStockRowFinancials()
    Dim ie As Object
    Dim htmlDoc As Object
    Dim targetTable As Object
    
    ' Initialize IE object
    Set ie = CreateObject("InternetExplorer.Application")
    ie.Visible = True ' Set to False if you don't want to see the browser window
    ie.Navigate "https://stockrow.com/AAPL/financials/income/annual"
    
    ' Wait for the page to fully load (including JS-rendered content)
    Do While ie.Busy Or ie.ReadyState <> 4
        DoEvents
    Loop
    
    ' Get the fully rendered HTML document
    Set htmlDoc = ie.Document
    
    ' Locate the financial table (adjust the selector based on your dev tools inspection)
    ' For example, if the table has a class like "table-financials":
    Set targetTable = htmlDoc.querySelector(".table-financials")
    
    ' Extract data from the table (example: print first row values)
    If Not targetTable Is Nothing Then
        Dim row As Object
        Dim cell As Object
        Set row = targetTable.Rows(1) ' Skip header row if needed
        For Each cell In row.Cells
            Debug.Print cell.innerText
        Next cell
    Else
        Debug.Print "Table not found!"
    End If
    
    ' Clean up
    ie.Quit
    Set ie = Nothing
End Sub

Note: You might need to adjust the CSS selector (.table-financials) to match the actual class/id of the table you're targeting—check your browser's dev tools for the correct selector.

Solution 2: Fetch data directly from the API (more efficient)

If you want a faster approach without using IE, you can look for the backend API that feeds the table data. Here's how:

  1. Open your browser's DevTools (F12) and go to the Network tab.
  2. Refresh the page and look for XHR/fetch requests that return JSON data (they'll often have keywords like "financials" or "api" in the URL).
  3. Once you find the API endpoint, you can use XMLHttpRequest to fetch the JSON directly, then parse it in VBA.

For parsing JSON, you'll need a library like VBA-JSON (you can import it into your VBA project). This method is more efficient because you're skipping the full page render and getting raw data directly.

Important Notes

  • Be mindful of the website's anti-scraping policies—don't send too many requests too quickly, or you might get your IP blocked.
  • Check the site's robots.txt file to make sure scraping is allowed for the data you're targeting.
  • If using IE, ensure your machine has IE installed (some newer Windows versions might require enabling it via Control Panel).

内容的提问来源于stack exchange,提问作者Kohlkopf

火山引擎 最新活动