我在 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/