首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >用Excel-VBA获取HTML源代码

用Excel-VBA获取HTML源代码
EN

Stack Overflow用户
提问于 2010-03-26 11:09:36
回答 2查看 22.7K关注 0票数 1

我想将excel VBA表单定向到某些URL,获得HTML源代码并将该资源存储在字符串中。这是可能的吗?如果可能,我该怎么做?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-05-17 06:08:17

只是对上述响应的补充。问题是如何获得所述答案实际上没有提供的HTML源代码。

将oXMLHTTP.responseText的内容与浏览器中URL "http://finance.yahoo.com/q/op?s=T+Options"“的源代码进行比较,两者不匹配,甚至返回的值也不一样。(这应该在几个小时后执行,以避免在交易日发生变化。)

如果我找到一种方法来执行这项任务的基本代码将被张贴。

票数 1
EN

Stack Overflow用户

发布于 2010-03-26 19:14:56

是。要做到这一点,一种方法是使用Tools->References.的MSXML DLL,为此,您需要通过Microsoft XML库添加一个引用

下面是一些显示给定URL内容的代码:

代码语言:javascript
复制
Public Sub ShowHTML(ByVal strURL)
    On Error GoTo ErrorHandler
    Dim strError As String
    strError = ""
    Dim oXMLHTTP As MSXML2.XMLHTTP
    Set oXMLHTTP = New MSXML2.XMLHTTP
    Dim strResponse As String
    strResponse = ""

    With oXMLHTTP
        .Open "GET", strURL, False
        .send ""
        If .Status <> 200 Then
            strError = .statusText
            GoTo CleanUpAndExit
        Else
            If .getResponseHeader("Content-type") <> "text/html" Then
                strError = "Not an HTML file"
                GoTo CleanUpAndExit
            Else
                strResponse = .responseText
            End If
        End If
    End With

CleanUpAndExit:
    On Error Resume Next ' Avoid recursive call to error handler
    ' Clean up code goes here
    Set oXMLHTTP = Nothing
    If Len(strError) > 0 Then ' Report any error
        MsgBox strError
    Else
        MsgBox strResponse
    End If
    Exit Sub
ErrorHandler:
    strError = Err.Description
    Resume CleanUpAndExit
End Sub
票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2520949

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档