【问题标题】:Can't fetch html element using createDocumentFromUrl无法使用 createDocumentFromUrl 获取 html 元素
【发布时间】:2017-09-04 09:40:16
【问题描述】:

如何使用“createDocumentFromUrl()”直接在 vba 中从网页中获取“HTMLDocument”?我尝试了很多以在 SO 中找到有关它的任何文档,但未能找到。希望有人伸出援助之手来实现这一目标。提前致谢。

这是我迄今为止尝试过的绝对不正确的方法:

Sub HtmlScraper()
    Dim odoc As Object
    Set odoc = New HTMLDocument

    odoc.Open createDocumentFromUrl("http://www.stackoverflow.com", "null")
    MsgBox odoc.body.innerHTML
End Sub

我也试过这样,但没有运气:

Sub htmlparser()
    Dim odoc As HTMLDocument, hdoc As HTMLDocument

    Set odoc = New HTMLDocument
    Set hdoc = New HTMLDocument

    Set hdoc = odoc.createDocumentFromUrl("http://www.stackoverflow.com", Null, False)
    MsgBox hdoc.body.outerHTML
End Sub

【问题讨论】:

  • 还要检查新文档的.readystate.busy 属性。

标签: vba dom web-scraping


【解决方案1】:

这对我有用,可能是网站。

Sub test()

Dim d As MSHTML.HTMLDocument
Set d = New MSHTML.HTMLDocument
Dim d2 As MSHTML.HTMLDocument

set d2=d.createDocumentFromUrl("www.bbc.co.uk", "null")

While d.readyState <> "complete"
    DoEvents
Wend

End Sub

【讨论】:

  • 似乎它正在工作,因为我在执行时没有遇到任何错误,但是如何获取 html 的东西。我的意思是在这种情况下,在哪里以及如何使用 debug.print 或 msgbox 来获取元素?谢谢。
  • 你用谷歌搜索过吗?还是使用文档对象中的智能感知?
  • 添加这一行 MsgBox d2.body.outerHTML 或 MsgBox d2.body.innerHTML
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-02-05
  • 2015-12-16
  • 1970-01-01
  • 1970-01-01
  • 2016-08-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多