html - 如何在不在 vba 中创建 Internet Explorer 对象的情况下解析 html?

标签 html vba excel internet-explorer

我工作时的任何一台计算机上都没有 Internet Explorer,因此无法创建 Internet Explorer 对象并使用 ie.navigate 解析 html 并搜索标签。我的问题是,如何在不使用 IE 的情况下自动将带有标签的某些数据从框架源提取到我的电子表格?答案中的代码示例将非常有用:)谢谢

最佳答案

您可以使用 XMLHTTP 检索网页的 HTML 源代码:

Function GetHTML(url As String) As String
    With CreateObject("MSXML2.XMLHTTP")
        .Open "GET", url, False
        .Send
        GetHTML = .ResponseText
    End With
End Function

我不建议将其用作工作表函数,否则每次工作表重新计算时都会重新查询站点 URL。一些网站有适当的逻辑来检测通过频繁、重复的调用进行的抓取,并且您的 IP 可能会暂时或永久被禁止,具体取决于网站。

获得源 HTML 字符串后(最好存储在变量中以避免不必要的重复调用),您可以使用基本的文本函数来解析字符串以搜索您的标签。

此基本函数将返回 <tag> 之间的值 </tag> :

Public Function getTag(url As String, tag As String, Optional occurNum As Integer) As String
    Dim html As String, pStart As Long, pEnd As Long, o As Integer
    html = GetHTML(url)

    'remove <> if they exist so we can add our own
    If Left(tag, 1) = "<" And Right(tag, 1) = ">" Then
        tag = Left(Right(tag, Len(tag) - 1), Len(Right(tag, Len(tag) - 1)) - 1)
    End If

    ' default to Occurrence #1
    If occurNum = 0 Then occurNum = 1
    pEnd = 1

    For o = 1 To occurNum
        ' find start <tag> beginning at 1 (or after previous Occurence)
        pStart = InStr(pEnd, html, "<" & tag & ">", vbTextCompare)
        If pStart = 0 Then
            getTag = "{Not Found}"
            Exit Function
        End If
        pStart = pStart + Len("<" & tag & ">")

        ' find first end </tag> after start <tag>
        pEnd = InStr(pStart, html, "</" & tag & ">", vbTextCompare)
    Next o

    'return string between start <tag> & end </tag>
    getTag = Mid(html, pStart, pEnd - pStart)
End Function

这只会找到基本的 <tag> ,但您可以添加/删除/更改文本功能以满足您的需要。

示例用法:

Sub findTagExample()

    Const testURL = "https://en.wikipedia.org/wiki/Web_scraping"

    'search for 2nd occurence of tag: <h2> which is "Contents" :
    Debug.Print getTag(testURL, "<h2>", 2)

    '...this returns the 8th occurence, "Navigation Menu" :
    Debug.Print getTag(testURL, "<h2>", 8)

    '...and this returns an HTML <span> containing a title for the 'Legal Issues' section:
    Debug.Print getTag("https://en.wikipedia.org/wiki/Web_scraping", "<h2>", 4)

End Sub

关于html - 如何在不在 vba 中创建 Internet Explorer 对象的情况下解析 html?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47592151/

相关文章:

javascript - 动态添加行到日期数组

javascript - 刷新页面而不出现屏幕空白?

excel - 从个人宏访问两个不同的工作簿

vba - VBA中重复值的问题

vba - Excel VBA : clear items in pivot table

javascript - 给定代码中 <svg> 和 <canvas> 实现有什么区别?

javascript - Angular/HTML - 将显示完整的 JSON 但不能仅显示单个属性

sql - 每个多组按列排列的前 N ​​个聚合值

excel - 使用 Python 突出显示 Excel 中的单元格

excel - 扫描列以查找匹配的文本并将结果粘贴到另一列中