【发布时间】:2021-10-15 09:03:17
【问题描述】:
运行 Excel 宏 VBA 时遇到错误。它说:对象变量或未设置块变量
Private Sub CommandButton1_Click()
Dim ie As InternetExplorer
Dim doc As HTMLDocument
Dim rating As String
Set ie = New InternetExplorer
With ie
.navigate "https://www.pse.com.ph/company-information-JFC/"
.Visible = False
Do While .readyState <> 4: DoEvents: Loop
Set doc = ie.document
With doc
rating = .getElementsByClassName("last-price")(0).PreviousSibling.getElementsByTagName("h3")(0).innerText
MsgBox rating
End With
End With
ie.Quit
End Sub
Excel 指向我这里:
rating = .getElementsByClassName("last-price")(0).PreviousSibling.getElementsByTagName("h3")(0).innerText
我浏览 Microsoft 文档。他们告诉我的变量没有设置。虽然乍一看,我的代码看起来不错。
请指教。
【问题讨论】:
-
要么没有“last-price”类的元素,要么有,但第一个没有
PreviousSibling,或者那个兄弟确实存在,但是没有 H3 子元素。都是一行,因此很难知道问题出在哪里:如果将其分解,则更容易排除故障。 -
看看那个页面,它在 IE 中被严重破坏了,所以也许你没有找到你要找的东西也就不足为奇了。
标签: excel vba web-scraping