如何从依赖于 Excel VBA 单元格值的动态 URL 访问网站?

问题描述 投票:0回答:1

我正在尝试使用 VBA 从雅虎财经获取股票的收盘价。我可以通过指定 URL 和类名来获取我的项目来实现此目的,但当我尝试使 url 依赖于单元格值时,它会以某种方式返回“424”错误(缺少对象)。基本上我希望能够根据单元格中写入的代码访问正确的页面。

这是工作代码,它获取收盘价并将其返回到单元格中:

Sub ExtractDataFromWebsite_BANB()
    Dim httpRequest As Object
    Dim htmlDocument As Object
    Dim htmlElement As Object
    Dim extractedData As String
    
    ' Create a new XMLHTTP request object
    Set httpRequest = CreateObject("MSXML2.XMLHTTP")
    
    ' Specify the URL of the website you want to scrape
    Dim websiteURL As String
    websiteURL = "https://finance.yahoo.com/quote/BANB.SW?"
    MsgBox websiteURL
    ' Send a request to the website
    httpRequest.Open "GET", websiteURL, False
    httpRequest.send
    
    ' Create an HTML document object and load the response text into it
    Set htmlDocument = CreateObject("htmlfile")
    htmlDocument.body.innerHTML = httpRequest.responseText
    
    ' Extract the desired data
    ' This example assumes you are looking for the first instance of an element with a specific class name
    Set htmlElement = htmlDocument.getElementsByClassName("Ta(end) Fw(600) Lh(14px)")(0)
    If Not htmlElement Is Nothing Then
        extractedData = htmlElement.innerText
        ' Output the extracted data to the Immediate Window (Debug.Print) and a message box
        Range("I1").Value = extractedData
    Else
        MsgBox "Data not found."
    End If
    
    ' Clean up
    Set httpRequest = Nothing
    Set htmlDocument = Nothing
    Set htmlElement = Nothing
End Sub

现在,我简单地对其进行了一些更改,尝试获取可以根据单元格的值动态更改的 url:

Sub ExtractDataFromWebsite_BANB()
    Dim httpRequest As Object
    Dim htmlDocument As Object
    Dim htmlElement As Object
    Dim extractedData As String
    
    ' Create a new XMLHTTP request object
    Set httpRequest = CreateObject("MSXML2.XMLHTTP")
    
    ' Specify the URL of the website you want to scrape
    Dim websiteURL As String
    websiteURL = "https://finance.yahoo.com/quote/" & Range("Q1").Value & "/"
    MsgBox websiteURL
    ' Send a request to the website
    httpRequest.Open "GET", websiteURL, False
    httpRequest.send
    
    ' Create an HTML document object and load the response text into it
    Set htmlDocument = CreateObject("htmlfile")
    htmlDocument.body.innerHTML = httpRequest.responseText
    
    ' Extract the desired data
    ' This example assumes you are looking for the first instance of an element with a specific class name
    Set htmlElement = htmlDocument.getElementsByClassName("Ta(end) Fw(600) Lh(14px)")(0)
    If Not htmlElement Is Nothing Then
        extractedData = htmlElement.innerText
        ' Output the extracted data to the Immediate Window (Debug.Print) and a message box
        Range("I1").Value = extractedData
    Else
        MsgBox "Data not found."
    End If
    
    ' Clean up
    Set httpRequest = Nothing
    Set htmlDocument = Nothing
    Set htmlElement = Nothing
End Sub

我尝试过使用 .Text 而不是 .Value,或者 + 而不是 &,但没有成功。当我调试时,URL 似乎是一个有效的字符串。我做错了什么?

excel vba yahoo-finance
1个回答
0
投票

股票代码的单元格类型(在本例中为 Q1)必须手动设置为“文本”而不是“常规”,这似乎可以修复它

© www.soinside.com 2019 - 2024. All rights reserved.