我无法在网页上检索信息

时间:2019-07-10 14:24:30

标签: vba extract

原理:加载页面并恢复标签a中包含的信息

这是源代码:

    <div class="w3-row w3-white w3-padding w3-hide-medium w3-hide-small" style="margin-top:5px;">

        <div class="w3-col s5 notranslate" style="margin:4px 0 6px 0">
            <a class="w3schools-logo" href="//www.w3schools.com">w3schools</a>
        </div>
  </div>

但是在执行代码时,我看到此错误:该对象不支持属性或方法

Private Function CreerNavigateur()
Dim IE As Object
Dim oDoc As Object
Dim Htable, maTable, hyper As Object
Dim text As String

Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.navigate "www.w3schools.com"
WaitIE IE
    ' Page chargée, on continue
   Set oDoc = IE.Document
   Set Htable = oDoc.getElementsByTagName("div")(1)
   Set maTable = Htable.getElementsByTagName("a")
   MsgBox maTable
   Set hyper = maTable.getElementsByClassName("w3schools-logo")
   text = hyper.innerText
   MsgBox text
 IE.Quit
    'On libère les variables
    Set IE = Nothing
    Set IEDoc = Nothing
End Function

1 个答案:

答案 0 :(得分:0)

您收到的错误是因为Htable.getElementsByTagName返回了一个元素集合,并且该集合没有方法maTable.getElementsByClassName。您需要引用集合中的第一项(使用maTable(0)),然后可以按以下方式访问hrefinnertext

Private Function CreerNavigateur()
Dim IE As Object
Dim oDoc As Object
Dim Htable, maTable, hyper As Object
Dim text As String

Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.navigate "www.w3schools.com"
WaitIE IE
    ' Page chargée, on continue
    Set oDoc = IE.Document
    Set Htable = oDoc.getElementsByTagName("div")(1)
    MsgBox Htable.innerhtml
    Set maTable = Htable.getElementsByTagName("a")
    MsgBox maTable(0).href
    MsgBox maTable(0).innertext
 IE.Quit
    'On libère les variables
    Set IE = Nothing
    Set IEDoc = Nothing
End Function