获取下拉列表选项

时间:2019-05-03 13:47:31

标签: excel vba web-scraping

我正在尝试从列表选项中提取数据

Sub Get_DropDown_List_Options()
Dim ie          As InternetExplorer
Dim html        As HTMLDocument
Dim drp         As HTMLFormElement
Dim x           As Long

Set ie = New InternetExplorer

ie.Visible = True
ie.navigate ThisWorkbook.Path & "\Sample.html"

Do While ie.readyState <> READYSTATE_COMPLETE: DoEvents: Loop
Set html = ie.document

Set drp = html.getElementById("ember-power-select-options-ember710")
x = html.forms.Length
MsgBox x 'Gives 0

 'How to extract the data from this list to excel column

Set ie = Nothing
End Sub

但是我无能为力..,这是一个示例html文件

<ul aria-controls="ember-power-select-trigger-ember710" role="listbox" id="ember-power-select-options-ember710" class="ember-power-select-options ember-view"><!----><li role="option" class="ember-power-select-group">
  <span class="ember-power-select-group-name">Welcome to the Excel ADVANCED Course!</span>
  <ul aria-controls="ember-power-select-trigger-ember710" role="group" id="ember1886" class="ember-power-select-options ember-view"><!---->    <li aria-selected="false" aria-current="false" data-option-index="0.0" role="option" class="ember-power-select-option">
                          A message from John Michaloudis!



    </li>
</ul>
</li><li role="option" class="ember-power-select-group">
  <span class="ember-power-select-group-name">ADVANCED FORMULA FUNDAMENTALS</span>
  <ul aria-controls="ember-power-select-trigger-ember710" role="group" id="ember1889" class="ember-power-select-options ember-view"><!---->    <li aria-selected="false" aria-current="true" data-option-index="1.0" role="option" class="ember-power-select-option">
                          Course Intro



    </li>
    <li aria-selected="false" aria-current="false" data-option-index="1.1" role="option" class="ember-power-select-option">
                          Download Workbook Instructions



    </li>
    <li aria-selected="false" aria-current="false" data-option-index="1.2" role="option" class="ember-power-select-option">
                          Download Workbook



    </li>
    <li aria-selected="false" aria-current="false" data-option-index="1.3" role="option" class="ember-power-select-option">
                          Relative Cell References



    </li>
    <li aria-selected="false" aria-current="false" data-option-index="1.4" role="option" class="ember-power-select-option">
                          Absolute Cell References



    </li>
    <li aria-selected="false" aria-current="false" data-option-index="1.5" role="option" class="ember-power-select-option">
                          Variations on Absolute References



    </li>
    <li aria-selected="false" aria-current="false" data-option-index="1.6" role="option" class="ember-power-select-option">
                          Name Ranges



    </li>
    <li aria-selected="false" aria-current="false" data-option-index="1.7" role="option" class="ember-power-select-option">
                          Nesting Functions



    </li>
</ul>
</li><li role="option" class="ember-power-select-group">
  <span class="ember-power-select-group-name">EVALUATING &amp; DEBUGGING FORMULAS</span>
  <ul aria-controls="ember-power-select-trigger-ember710" role="group" id="ember1892" class="ember-power-select-options ember-view"><!---->    <li aria-selected="false" aria-current="false" data-option-index="2.0" role="option" class="ember-power-select-option">
                          Course Intro



    </li>
    <li aria-selected="false" aria-current="false" data-option-index="2.1" role="option" class="ember-power-select-option">
                          Download Workbook



    </li>
    <li aria-selected="false" aria-current="false" data-option-index="2.2" role="option" class="ember-power-select-option">
                          Evaluate Formulas



    </li>
    <li aria-selected="false" aria-current="false" data-option-index="2.3" role="option" class="ember-power-select-option">
                          Locating Errors in a Formula



    </li>
    <li aria-selected="false" aria-current="false" data-option-index="2.4" role="option" class="ember-power-select-option">
                          Using Excel's IFERROR() Function



    </li>
</ul>
</li><li role="option" class="ember-power-select-group">
  <span class="ember-power-select-group-name">ARRAY FORMULAS</span>
  <ul aria-controls="ember-power-select-trigger-ember710" role="group" id="ember1895" class="ember-power-select-options ember-view"><!---->    <li aria-selected="false" aria-current="false" data-option-index="3.0" role="option" class="ember-power-select-option">
                          Course Intro



    </li>
    <li aria-selected="false" aria-current="false" data-option-index="3.1" role="option" class="ember-power-select-option">
                          Download Workbook



    </li>
    <li aria-selected="false" aria-current="false" data-option-index="3.2" role="option" class="ember-power-select-option">
                          What are Arrays?



    </li>
    <li aria-selected="false" aria-current="false" data-option-index="3.3" role="option" class="ember-power-select-option">
                          Nesting Array Formulas



    </li>
    <li aria-selected="false" aria-current="false" data-option-index="3.4" role="option" class="ember-power-select-option">
                          Using Arrays to SUM Data



    </li>
    <li aria-selected="false" aria-current="false" data-option-index="3.5" role="option" class="ember-power-select-option">
                          Streamline Conditions Using Arrays



    </li>
</ul>
</li><li role="option" class="ember-power-select-group">
  <span class="ember-power-select-group-name">FUNDAMENTALS OF USING THE INDEX() AND MATCH() FUNCTIONS</span>
  <ul aria-controls="ember-power-select-trigger-ember710" role="group" id="ember1898" class="ember-power-select-options ember-view"><!---->    <li aria-selected="false" aria-current="false" data-option-index="4.0" role="option" class="ember-power-select-option">
                          Course Intro



    </li>
    <li aria-selected="false" aria-current="false" data-option-index="4.1" role="option" class="ember-power-select-option">
                          Download Workbook



    </li>
    <li aria-selected="false" aria-current="false" data-option-index="4.2" role="option" class="ember-power-select-option">
                          INDEX() Function



    </li>
    <li aria-selected="false" aria-current="false" data-option-index="4.3" role="option" class="ember-power-select-option">
                          MATCH() Function



    </li>
    <li aria-selected="false" aria-current="false" data-option-index="4.4" role="option" class="ember-power-select-option">
                          Nesting INDEX() and MATCH()



    </li>
</ul>
</li><li role="option" class="ember-power-select-group">
  <span class="ember-power-select-group-name">ADVANCED INDEX() AND MATCH() EXCERCISES</span>
  <ul aria-controls="ember-power-select-trigger-ember710" role="group" id="ember1901" class="ember-power-select-options ember-view"><!---->    <li aria-selected="false" aria-current="false" data-option-index="5.0" role="option" class="ember-power-select-option">
                          Course Intro



    </li>
    <li aria-selected="false" aria-current="false" data-option-index="5.1" role="option" class="ember-power-select-option">
                          Download Workbook



    </li>
    <li aria-selected="false" aria-current="false" data-option-index="5.2" role="option" class="ember-power-select-option">
                          Creating a Dynamic SUM() Using INDEX() - Part 1



    </li>
    <li aria-selected="false" aria-current="false" data-option-index="5.3" role="option" class="ember-power-select-option">
                          Creating a Dynamic SUM() Using INDEX() - Part 2



    </li>
    <li aria-selected="false" aria-current="false" data-option-index="5.4" role="option" class="ember-power-select-option">
                          Combine Multiplication with INDEX() and MATCH() - Part 1



    </li>
    <li aria-selected="false" aria-current="false" data-option-index="5.5" role="option" class="ember-power-select-option">
                          Combine Multiplication with INDEX() and MATCH() - Part 2



    </li>
    <li aria-selected="false" aria-current="false" data-option-index="5.6" role="option" class="ember-power-select-option">
                          Create a Dynamic VLOOKUP() with MATCH() - Part 1



    </li>
    <li aria-selected="false" aria-current="false" data-option-index="5.7" role="option" class="ember-power-select-option">
                          Create a Dynamic VLOOKUP() with MATCH() - Part 2



    </li>
    <li aria-selected="false" aria-current="false" data-option-index="5.8" role="option" class="ember-power-select-option">
                          Create a Dynamic VLOOKUP() with MATCH() - Part 3



    </li>
    <li aria-selected="false" aria-current="false" data-option-index="5.9" role="option" class="ember-power-select-option">
                          Turn INDEX() and MATCH() into an ARRAY - Part 1



    </li>
    <li aria-selected="false" aria-current="false" data-option-index="5.10" role="option" class="ember-power-select-option">
                          Turn INDEX() and MATCH() into an ARRAY - Part 2



    </li>
    <li aria-selected="false" aria-current="false" data-option-index="5.11" role="option" class="ember-power-select-option">
                          Returning Multiple Values with INDEX() - Part 1



    </li>
    <li aria-selected="false" aria-current="false" data-option-index="5.12" role="option" class="ember-power-select-option">
                          Returning Multiple Values with INDEX() - Part 2



    </li>
    <li aria-selected="false" aria-current="false" data-option-index="5.13" role="option" class="ember-power-select-option">
                          Returning Multiple Values with INDEX() - Part 3



    </li>
    <li aria-selected="false" aria-current="false" data-option-index="5.14" role="option" class="ember-power-select-option">
                          Returning Multiple Values with INDEX() - Part 4



    </li>
    <li aria-selected="false" aria-current="false" data-option-index="5.15" role="option" class="ember-power-select-option">
                          Adding Criteria to INDEX() and MATCH() - Part 1



    </li>
    <li aria-selected="false" aria-current="false" data-option-index="5.16" role="option" class="ember-power-select-option">
                          Adding Criteria to INDEX() and MATCH() - Part 2



    </li>
    <li aria-selected="false" aria-current="false" data-option-index="5.17" role="option" class="ember-power-select-option">
                          Return an Associated Value Using INDEX() and MATCH() - Part 1



    </li>
    <li aria-selected="false" aria-current="false" data-option-index="5.18" role="option" class="ember-power-select-option">
                          Return an Associated Value Using INDEX() and MATCH() - Part 2



    </li>
    <li aria-selected="false" aria-current="false" data-option-index="5.19" role="option" class="ember-power-select-option">
                          Conditional Formatting Using MATCH() - Part 1



    </li>
    <li aria-selected="false" aria-current="false" data-option-index="5.20" role="option" class="ember-power-select-option">
                          Conditional Formatting Using MATCH() - Part 2



    </li>
</ul>
</li><li role="option" class="ember-power-select-group">
  <span class="ember-power-select-group-name">TEXT FORMULAS</span>
  <ul aria-controls="ember-power-select-trigger-ember710" role="group" id="ember1904" class="ember-power-select-options ember-view"><!---->    <li aria-selected="false" aria-current="false" data-option-index="6.0" role="option" class="ember-power-select-option">
                          Download Workbooks



    </li>
    <li aria-selected="false" aria-current="false" data-option-index="6.1" role="option" class="ember-power-select-option">
                          CLEAN - Intro



    </li>
    <li aria-selected="false" aria-current="false" data-option-index="6.2" role="option" class="ember-power-select-option">
                          FIND - Intro



    </li>
    <li aria-selected="false" aria-current="false" data-option-index="6.3" role="option" class="ember-power-select-option">
                          MID - Extract Numbers Between Hyphens



    </li>
    <li aria-selected="false" aria-current="false" data-option-index="6.4" role="option" class="ember-power-select-option">
                          PROPER - Intro



    </li>
    <li aria-selected="false" aria-current="false" data-option-index="6.5" role="option" class="ember-power-select-option">
                          REPLACE - Intro



    </li>
    <li aria-selected="false" aria-current="false" data-option-index="6.6" role="option" class="ember-power-select-option">
                          REPLACE - Phone Area Codes



    </li>
    <li aria-selected="false" aria-current="false" data-option-index="6.7" role="option" class="ember-power-select-option">
                          REPLACE - Insert Hyphens In Your Serial Number



    </li>
    <li aria-selected="false" aria-current="false" data-option-index="6.8" role="option" class="ember-power-select-option">
                          REPLACE - Extract Last Name



    </li>
    <li aria-selected="false" aria-current="false" data-option-index="6.9" role="option" class="ember-power-select-option">
                          REPLACE - Clear Everything Before the Hyphen



    </li>
    <li aria-selected="false" aria-current="false" data-option-index="6.10" role="option" class="ember-power-select-option">
                          REPLACE - Insert New Code within Old Part Numbers



    </li>
    <li aria-selected="false" aria-current="false" data-option-index="6.11" role="option" class="ember-power-select-option">
                          SUBSTITUTE - Intro



    </li>
    <li aria-selected="false" aria-current="false" data-option-index="6.12" role="option" class="ember-power-select-option">
                          SUBSTITUTE - Add a Comma after the Surname



    </li>
    <li aria-selected="false" aria-current="false" data-option-index="6.13" role="option" class="ember-power-select-option">
                          SUBSTITUTE - Remove Second Hyphen



    </li>
    <li aria-selected="false" aria-current="false" data-option-index="6.14" role="option" class="ember-power-select-option">
                          SUBSTITUTE - Count Text Occurrences in a Cell



    </li>
    <li aria-selected="false" aria-current="false" data-option-index="6.15" role="option" class="ember-power-select-option">
                          SUBSTITUTE - Report Name



    </li>
    <li aria-selected="false" aria-current="false" data-option-index="6.16" role="option" class="ember-power-select-option">
                          TEXT - Intro



    </li>
    <li aria-selected="false" aria-current="false" data-option-index="6.17" role="option" class="ember-power-select-option">
                          TRIM - Intro



    </li>
</ul>
</li><li role="option" class="ember-power-select-group">
  <span class="ember-power-select-group-name">TEXT TIPS!</span>
  <ul aria-controls="ember-power-select-trigger-ember710" role="group" id="ember1907" class="ember-power-select-options ember-view"><!---->    <li aria-selected="false" aria-current="false" data-option-index="7.0" role="option" class="ember-power-select-option">
                          Download Workbooks



    </li>
    <li aria-selected="false" aria-current="false" data-option-index="7.1" role="option" class="ember-power-select-option">
                          Convert Formulas to Values



    </li>
    <li aria-selected="false" aria-current="false" data-option-index="7.2" role="option" class="ember-power-select-option">
                          Convert Text to Numbers



    </li>
</ul>
</li><li role="option" class="ember-power-select-group">
  <span class="ember-power-select-group-name">TEXT ARRAY FORMULAS</span>
  <ul aria-controls="ember-power-select-trigger-ember710" role="group" id="ember1910" class="ember-power-select-options ember-view"><!---->    <li aria-selected="false" aria-current="false" data-option-index="8.0" role="option" class="ember-power-select-option">
                          Download Workbook



    </li>
    <li aria-selected="false" aria-current="false" data-option-index="8.1" role="option" class="ember-power-select-option">
                          MID {ARRAY FORMULA} - Extract Cell Contents in Separate Rows



    </li>
</ul>
</li><li role="option" class="ember-power-select-group">
  <span class="ember-power-select-group-name">ADVANCED VLOOKUP</span>
  <ul aria-controls="ember-power-select-trigger-ember710" role="group" id="ember1913" class="ember-power-select-options ember-view"><!---->    <li aria-selected="false" aria-current="false" data-option-index="9.0" role="option" class="ember-power-select-option">
                          Download Workbooks



    </li>
    <li aria-selected="false" aria-current="false" data-option-index="9.1" role="option" class="ember-power-select-option">
                          VLOOKUP - A TAX TABLE WITH APPROXIMATE MATCH



    </li>
    <li aria-selected="false" aria-current="false" data-option-index="9.2" role="option" class="ember-power-select-option">
                          VLOOKUP - SUM MULTIPLE COLUMNS WITH DATA VALIDATION DROP DOWN MENUS



    </li>
    <li aria-selected="false" aria-current="false" data-option-index="9.3" role="option" class="ember-power-select-option">
                          VLOOKUP - MULTIPLE CRITERIA



    </li>
</ul>
</li><li role="option" class="ember-power-select-group">
  <span class="ember-power-select-group-name">SUMIFS</span>
  <ul aria-controls="ember-power-select-trigger-ember710" role="group" id="ember1916" class="ember-power-select-options ember-view"><!---->    <li aria-selected="false" aria-current="false" data-option-index="10.0" role="option" class="ember-power-select-option">
                          Download Workbook



    </li>
    <li aria-selected="false" aria-current="false" data-option-index="10.1" role="option" class="ember-power-select-option">
                          SUMIFS WITH DATA VALIDATION DROP DOWN MENUS



    </li>
</ul>
</li><li role="option" class="ember-power-select-group">
  <span class="ember-power-select-group-name">NEW EXCEL 2019 FORMULAS</span>
  <ul aria-controls="ember-power-select-trigger-ember710" role="group" id="ember1919" class="ember-power-select-options ember-view"><!---->    <li aria-selected="false" aria-current="false" data-option-index="11.0" role="option" class="ember-power-select-option">
                          Download Workbooks



    </li>
    <li aria-selected="false" aria-current="false" data-option-index="11.1" role="option" class="ember-power-select-option">
                          CONCAT



    </li>
    <li aria-selected="false" aria-current="false" data-option-index="11.2" role="option" class="ember-power-select-option">
                          IFS



    </li>
    <li aria-selected="false" aria-current="false" data-option-index="11.3" role="option" class="ember-power-select-option">
                          MAXIFS



    </li>
    <li aria-selected="false" aria-current="false" data-option-index="11.4" role="option" class="ember-power-select-option">
                          SWITCH



    </li>
    <li aria-selected="false" aria-current="false" data-option-index="11.5" role="option" class="ember-power-select-option">
                          TEXTJOIN



    </li>
</ul>
</li><li role="option" class="ember-power-select-group">
  <span class="ember-power-select-group-name">NEW OFFICE 365 FORMULAS</span>
  <ul aria-controls="ember-power-select-trigger-ember710" role="group" id="ember1922" class="ember-power-select-options ember-view"><!---->    <li aria-selected="false" aria-current="false" data-option-index="12.0" role="option" class="ember-power-select-option">
                          Download Workbooks



    </li>
    <li aria-selected="false" aria-current="false" data-option-index="12.1" role="option" class="ember-power-select-option">
                          FILTER



    </li>
    <li aria-selected="false" aria-current="false" data-option-index="12.2" role="option" class="ember-power-select-option">
                          RANDARRAY



    </li>
    <li aria-selected="false" aria-current="false" data-option-index="12.3" role="option" class="ember-power-select-option">
                          SEQUENCE



    </li>
    <li aria-selected="false" aria-current="false" data-option-index="12.4" role="option" class="ember-power-select-option">
                          SORT



    </li>
    <li aria-selected="false" aria-current="false" data-option-index="12.5" role="option" class="ember-power-select-option">
                          SORTBY



    </li>
    <li aria-selected="false" aria-current="false" data-option-index="12.6" role="option" class="ember-power-select-option">
                          UNIQUE



    </li>
</ul>
</li></ul>

1 个答案:

答案 0 :(得分:1)

使用类名。根据要求进行编辑以包含标题。

Set options = ie.document.querySelectorAll(".ember-power-select-group-name, .ember-power-select-option")

从文件读取html:

Option Explicit
Public Sub Test()
    Dim html As HTMLDocument, options As Object, i As Long, r As Long, c As Long
    Set html = GetHTMLFileContent("C:\Users\User\Desktop\test.html")
    Set options = html.querySelectorAll(".ember-power-select-group-name, .ember-power-select-option")
    r = 1
    For i = 0 To options.Length - 1
        Select Case options.item(i).className
        Case "ember-power-select-group-name"
            c = 1
        Case "ember-power-select-option"
            c = 2
        End Select
        r = r + 1
        ActiveSheet.Cells(r, c) = options.item(i).innerText
    Next
End Sub

Public Function GetHTMLFileContent(ByVal filePath As String) As HTMLDocument
    Dim fso As Object, hFile As Object, hString As String, html As HTMLDocument
    Set html = New HTMLDocument
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set hFile = fso.OpenTextFile(filePath)

    Do Until hFile.AtEndOfStream
        hString = hFile.ReadAll()
    Loop

    html.body.innerHTML = hString
    Set GetHTMLFileContent = html
End Function

示例输出:

enter image description here