vba - 无法从网页中获取标题

标签 vba excel web-scraping internet-explorer-11

我在 vba 中结合 IE 编写了一个脚本获取 titles不同的charts从网页,但我无法。看来我用对了class名称以及 tag名称到达内容但没有骰子。它也不会抛出任何错误。

到目前为止,这是我的方法:

Sub GetTitle()
    Const Url As String = "https://www.fbatoolkit.com/"
    Dim IE As New InternetExplorer, Html As HTMLDocument
    Dim post As Object

    With IE
        .Visible = True
        .navigate Url
        While .Busy = True Or .readyState < 4: DoEvents: Wend
        Set Html = .document
    End With

    Application.Wait Now + TimeValue("00:00:05")

    For Each post In Html.getElementsByClassName("chart")
        With post.getElementsByTagName("text")
          If .Length Then R = R + 1: Cells(R, 1) = .item(0).innerText
        End With
    Next post
End Sub

标题如下所示,在每个图表上方可见:
Toys & Games
Health & Household

我不期待任何与 selenium 相关的解决方案.谢谢。

最佳答案

老实说,这有点作弊。将其视为占位符,直到我找到更好的方法,因为我猜您特别想访问 那些标题。

Option Explicit
Public Sub GetInfo()
    Dim ie As New InternetExplorer, html As HTMLDocument, titles(), i As Long
    With ie
        .Visible = True
        .navigate "https://www.fbatoolkit.com/"
        While .Busy Or .readyState < 4: DoEvents: Wend
        Set html = .document
        titles = GetTitles(html.body.innerHTML, "id=""visualization([^""]*)")
        For i = LBound(titles) To UBound(titles)
            Debug.Print titles(i)
        Next
        .Quit '<== Remember to quit application
    End With
End Sub

Public Function GetTitles(ByVal inputString As String, ByVal sPattern As String) As Variant
    Dim Matches As Object, iMatch As Object, s As String, arrMatches(), i As Long
    With CreateObject("vbscript.regexp")
        .Global = True
        .MultiLine = True
        .IgnoreCase = True
        .Pattern = sPattern
        If .test(inputString) Then
            Set Matches = .Execute(inputString)
            For Each iMatch In Matches
                If iMatch.SubMatches(0) <> vbNullString Then
                    ReDim Preserve arrMatches(i)
                    arrMatches(i) = Replace$(Replace$(iMatch.SubMatches(0), Chr$(95), Chr$(32)), Chr$(32) & Chr$(32), Chr$(32) & Chr$(38) & Chr$(32))
                    i = i + 1
                End If
            Next iMatch
        End If
    End With
    GetTitles = arrMatches
End Function

关于vba - 无法从网页中获取标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51579827/

相关文章:

vba - 迭代范围内的单元格时出现类型不匹配错误

python - BeautifulSoup4 查找所有非嵌套匹配项

python - 如何在python中从网络抓取的数据写入csv文件

excel - 如何在 VBA/excel 中正确转义范围

excel - 检查单元格范围没有范围名称后为其命名

c# - 如何通过 C# 在 Excel 中将公式值解析为文本

excel - 如何在excel中的折线图中添加垂直线?

python - 在 Python-Scrapy 中执行 Js 的 Selenium 替代品是什么?

excel - 在 A 列中查找测试 1-测试 4 后如何对 B 列中的值求和

excel - 使用宏复制每 N 行