【问题标题】:How to fetch iframe data using Excel VBA如何使用 Excel VBA 获取 iframe 数据
【发布时间】:2021-05-06 09:59:21
【问题描述】:

我在 Excel VBA 中使用下面提到的代码进行 IE 导航。从 iframe 获取数据时遇到以下错误。

错误详情:

对象不支持该属性或方法


Option Explicit

Public Sub Cgg_Click()
    Dim Ie As New InternetExplorer
    Dim WebURL
    Dim Docx As HTMLDocument
    Dim productDesc
    Dim productTitle
    Dim price
    Dim RcdNum
    
    Ie.Visible = True
    WebURL = "https://www.google.com/maps/place/parlour+beauty+parlour+beauty/@40.7314166,-74.13182,11z/data=!4m8!1m2!2m1!1sParlour+NY!3m4!1s0x89c2599bd4c1d2e7:0x20873676f6334189!8m2!3d40.7314166!4d-73.9917443"
    Ie.Navigate2 WebURL
    
    Do Until Ie.readyState = READYSTATE_COMPLETE
        DoEvents
    Loop
    
    Application.Wait (Now + TimeValue("00:00:25"))
    
    For N = 0 To Ie.document.getElementsByClassName("section-subheader-header GLOBAL__gm2-subtitle-alt-1").Length - 1
                       
        If Ie.document.getElementsByClassName("section-subheader-header GLOBAL__gm2-subtitle-alt-1").Item(N).innerText = "Web results" Then
            Ie.document.getElementsByClassName("section-subheader-header GLOBAL__gm2-subtitle-alt-1").Item(N).ScrollIntoView (False)
        End If
                    
    Next N
    
    Application.Wait (Now + TimeValue("00:00:25"))
         
    Set Docx = Ie.document
    
    productDesc = Docx.Window.frames("section-iframe-iframe").contentWindow.document.getElementsByClassName("trex")(0).outerHTML

End Sub

这是 HTML:


请帮助解决此错误。

我想从上面的 url 中提取 "trex" ClassName HTML Contain

谢谢。

【问题讨论】:

标签: html vba internet-explorer web-scraping iframe


【解决方案1】:

您可以将提取“trex”元素的行更改为以下之一,它们都可以正常工作:

  1. 先使用getElementsbyTagName方法获取Iframe,然后根据Iframe.contentDocument属性通过类名到达元素:

    productDesc = Docx.getElementsByTagName("iframe")(0).contentDocument.getElementsByClassName("trex")(0).outerHTML
    
  2. 使用querySelector方法通过类获取iframe,然后使用同上方法到达元素:

    productDesc = Docx.querySelector(".section-iframe-iframe").contentDocument.getElementsByClassName("trex")(0).outerHTML
    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-02-08
    • 1970-01-01
    • 2020-07-14
    • 2013-11-27
    • 1970-01-01
    • 1970-01-01
    • 2019-02-14
    • 1970-01-01
    相关资源
    最近更新 更多