取决于废料的已填充下拉列表

时间:2019-01-30 19:28:11

标签: excel vba web-scraping drop-down-menu

我希望使用Excel VBA从两个下拉列表中删除值,其中一个填充州名称,另一个填充城市。 我可以取消州名,但是当我尝试取消城市名时,一无所获。城市列表将根据选定的州进行相应填充。 如何在第一个列表的每个州的第二个下拉列表中列出每个城市?

此查询仅向我提供状态名称和第二个列表的默认值

Sub ScrapDropDown()
    Const URL As String = "http://idebescola.inep.gov.br/ideb/consulta-publica"
    Dim XMLPage As New MSXML2.XMLHTTP60
    Dim HTMLDoc As New MSHTML.HTMLDocument
    XMLPage.Open "GET", URL, False
    XMLPage.send
    HTMLDoc.body.innerHTML = XMLPage.responseText
    Set HTMLDocment = HTMLDoc.getElementById("pkCodEstado")
    For i = 1 To HTMLDocment.Length - 1
        Set HTMLpkCodMunicipio = HTMLDoc.getElementById("pkCodMunicipio")
        For Each HTMLMun In HTMLpkCodMunicipio.getElementsByTagName("option")
            Debug.Print i & "-" & HTMLDocment(i).Value & "-" & HTMLDocment(i).innerText & "-" & HTMLMun.Value & "-" & HTMLMun.innerText
        Next HTMLMun
    Next i
End Sub

我要剪贴的HTML带有拖放列表的一部分(我删除了三个不需要的列表的三个点),其中(在站点上)从第一个列表中选择状态,而不选择id =“ pkCodMunicipio”只有一个选项

<form method="post" name="frm" class="classForm" id="frm">
<label for="pkCodEntidade">Por Código</label>
<div class="divRequired">
</div>
<input name="pkCodEntidade" id="pkCodEntidade" placeholder="Código da Escola" title="Por Código" class="onlynumbers" maxlength="8" tabindex="15" type="text" value="">
<hr>
<label id="lbl">Por área de interesse</label>
<div id="lblDivRequired" class="divRequired" style="display: ;">
</div>
<select name="pkCodEstado" id="pkCodEstado" tabindex="16">
<option value="">UF</option>
<option value="12">ACRE</option>
<option value="27">ALAGOAS</option>
<option value="16">AMAPÁ</option>
<option value="13">AMAZONAS</option>
<option value="29">BAHIA</option>
<option value="23">CEARÁ</option>
<option value="53">DISTRITO FEDERAL</option>
<option value="32">ESPÍRITO SANTO</option>
<option value="52">GOIÁS</option>
<option value="21">MARANHÃO</option>
<option value="51">MATO GROSSO</option>
<option value="50">MATO GROSSO DO SUL</option>
<option value="31">MINAS GERAIS</option>
<option value="15">PARÁ</option>
<option value="25">PARAÍBA</option>
<option value="41">PARANÁ</option>
<option value="26">PERNAMBUCO</option>
<option value="22">PIAUÍ</option>
<option value="33">RIO DE JANEIRO</option>
<option value="24">RIO GRANDE DO NORTE</option>
<option value="43">RIO GRANDE DO SUL</option>
<option value="11">RONDÔNIA</option>
<option value="14">RORAIMA</option>
<option value="42">SANTA CATARINA</option>
<option value="35">SÃO PAULO</option>
<option value="28">SERGIPE</option>
<option value="17">TOCANTINS</option>
</select>
<select name="pkCodMunicipio" id="pkCodMunicipio" tabindex="17">
<option value="">Municípios</option>
<option value="1400050">ALTO ALEGRE</option>
<option value="1400027">AMAJARI</option>
<option value="1400100">BOA VISTA</option>
<option value="1400159">BONFIM</option>
<option value="1400175">CANTA</option>
<option value="1400209">CARACARAI</option>
<option value="1400233">CAROEBE</option>
<option value="1400282">IRACEMA</option>
<option value="1400308">MUCAJAI</option>
<option value="1400407">NORMANDIA</option>
<option value="1400456">PACARAIMA</option>
<option value="1400472">RORAINOPOLIS</option>
<option value="1400506">SAO JOAO DA BALIZA</option>
<option value="1400605">SAO LUIZ</option>
<option value="1400704">UIRAMUTA</option>
</select>
...
<button name="btnSearch" class="btnDefault btn btn-warning" title="Buscar" type="submit" id="btnSearch" onclick="void(0);">Buscar</button>
</div>
<input type="hidden" name="undefined" value="undefined">
</form>

1 个答案:

答案 0 :(得分:0)

您可以使用CSS选择器组合。下面使用id([['Danny Amendola'], 'Questionable: hamstring, week_1, Questionable: groin, week_2, Doubtful: groin, week_3, Questionable: groin, week_4, Questionable: groin, week_5, Probable: groin, week_6, Out: concussion, week_7, Questionable: concussion, week_8, Questionable: groin, week_9, Probable: groin, week_11, Probable: groin, week_12, Probable: groin, week_13, Probable: groin, week_14, Probable: groin, week_15, Questionable: groin, week_16, Probable: groin, week_17'] [['Armond Armstead'], 'Out: infection, week_1, Out: infection, week_2, Out: infection, week_3, Out: infection, week_4, Out: infection, week_5, Out: infection, week_6, Out: infection, week_7, Out: infection, week_8, Out: infection, week_9, Out: infection, week_11, Out: infection, week_12, Out: infection, week_13, Out: infection, week_14, Out: infection, week_15, Out: infection, week_16, Out: infection, week_17, Out: infection, week_19, Out: infection, week_20'] [['Kyle Arrington'], 'Questionable: groin, week_4, Questionable: groin, week_5, Probable: groin, week_6, Probable: groin, week_7, Probable: groin, week_8, Questionable: groin, week_9, Questionable: groin, week_11, Probable: groin, week_12, Questionable: groin, week_13, Questionable: groin, week_14, Questionable: groin, week_15, Questionable: groin, week_16, Questionable: groin, week_17'] [['Brandon Bolden'], 'Questionable: knee, week_1, Questionable: knee, week_2, Questionable: knee, week_3, Questionable: knee, week_4, Questionable: knee, week_5, Probable: knee, week_6, Questionable: knee, week_7, Questionable: knee, week_8, Questionable: knee, week_9, Questionable: knee, week_11'] [['Josh Boyce'], 'Doubtful: hip, week_16, Questionable: hip, week_17'] [['Tom Brady'], 'Probable: right shoulder, week_8, Probable: right shoulder, week_9, Probable: right shoulder, week_11, Probable: right shoulder, week_12, Probable: shoulder, week_13, Probable: right shoulder, week_14, Questionable: shoulder, week_15, Probable: right shoulder, week_16, Probable: right shoulder, week_17'] [['Marcus Cannon'], 'Questionable: shoulder, week_7, Questionable: shoulder, week_8, Questionable: shoulder, week_9, Questionable: ankle, week_13, Questionable: ankle, week_14, Questionable: ankle, week_15, Questionable: ankle, week_16, Questionable: ankle, week_17'] [['Marquice Cole'], 'Probable: hamstring, week_2, Questionable: hamstring, week_4, Questionable: hamstring, week_5, Questionable: leg, week_13, Questionable: shin, week_14, Questionable: shin, week_15'] [['Austin Collie'], 'N/A'] [['Dan Connolly'], 'Questionable: finger, week_3, Questionable: head, week_7'] [['Alfonzo Dennard'], 'Probable: ankle, week_2, Questionable: leg, week_11, Questionable: knee, week_13, Questionable: knee, week_14, Questionable: knee/shoulder, week_15, Questionable: knee/shoulder, week_16, Questionable: knee/shoulder, week_17'] [['Aaron Dobson'], 'Questionable: hamstring, week_1, Questionable: hamstring, week_2, Doubtful: shoulder, week_4, Questionable: neck, week_5, Questionable: neck, week_6, Questionable: undisclosed, week_13, Questionable: foot, week_14, Questionable: foot, week_15, Questionable: foot, week_16, Questionable: foot, week_17'] [['Nate Ebner'], 'Questionable: ankle, week_1, Questionable: ankle, week_2, Questionable: ankle, week_3, Questionable: ankle, week_4, Questionable: ankle, week_5, Probable: ankle, week_6'] [['Julian Edelman'], 'Questionable: thigh, week_7, Questionable: thigh, week_8, Probable: thigh, week_9'] [['Dane Fletcher'], 'Questionable: groin, week_16, Questionable: groin, week_17'] [['Tyronne Green'], 'Injured Reserve: undisclosed, week_1, Injured Reserve: undisclosed, week_2, Injured Reserve: undisclosed, week_3, Injured Reserve: undisclosed, week_4, Injured Reserve: undisclosed, week_5, Injured Reserve: undisclosed, week_6, Injured Reserve: undisclosed, week_7, Injured Reserve: undisclosed, week_8, Injured Reserve: undisclosed, week_9, Injured Reserve: undisclosed, week_11, Injured Reserve: undisclosed, week_12, Injured Reserve: undisclosed, week_13, Injured Reserve: undisclosed, week_14, Injured Reserve: undisclosed, week_15, Injured Reserve: undisclosed, week_16, Injured Reserve: undisclosed, week_17, Injured Reserve: undisclosed, week_19, Injured Reserve: undisclosed, week_20'] [['Steve Gregory'], 'Out: thumb, week_11, Questionable: finger, week_12, Questionable: finger, week_13, Questionable: finger, week_14, Questionable: finger, week_15, Questionable: finger, week_16, Questionable: knee/finger, week_17'] [['Cory Grissom'], 'Injured Reserve: knee, week_1, Injured Reserve: knee, week_2, Injured Reserve: knee, week_3, Injured Reserve: knee, week_4, Injured Reserve: knee, week_5, Injured Reserve: knee, week_6, Injured Reserve: knee, week_7, Injured Reserve: knee, week_8, Injured Reserve: knee, week_9, Injured Reserve: knee, week_11, Injured Reserve: knee, week_12, Injured Reserve: knee, week_13, Injured Reserve: knee, week_14, Injured Reserve: knee, week_15, Injured Reserve: knee, week_16, Injured Reserve: knee, week_17, Injured Reserve: knee, week_19, Injured Reserve: knee, week_20'] [['Rob Gronkowski'], 'Doubtful: arm/back, week_1, Questionable: arm/back, week_2, Doubtful: arm/back, week_3, Questionable: arm/back, week_4, Doubtful: arm/back, week_5, Probable: arm/back, week_6, Questionable: arm/back, week_7, Probable: back/forearm, week_8, Probable: back/forearm/hamstring, week_9, Probable: back/forearm/hamstring, week_11, Probable: back/forearm/hamstring, week_12, Probable: hamstring, week_13, Questionable: ankle, week_14, Injured Reserve: torn right ACL/MCL, week_15, Injured Reserve: torn right ACL/MCL, week_16, Injured Reserve: torn right ACL/MCL, week_17, Injured Reserve: torn right ACL/MCL, week_19, Injured Reserve: torn right ACL/MCL, week_20'] [['Duron Harmon'], 'Questionable: hamstring, week_1, Questionable: hamstring, week_2'] [['Mark Harrison'], 'Out: foot, week_1, Out: foot, week_2, Out: foot, week_3, Out: foot, week_4, Out: foot, week_5, Out: foot, week_6, Out: foot, week_7, Out: foot, week_8, Out: foot, week_9, Out: foot, week_11, Out: foot, week_12, Out: foot, week_13, Out: foot, week_14, Out: foot, week_15, Out: foot, week_16, Out: foot, week_17, Out: foot, week_19, Out: foot, week_20'] [["Dont'a Hightower"], 'Questionable: knee, week_5, Probable: knee, week_6'] [['Michael Hoomanawanui'], 'Questionable: knee, week_7, Questionable: knee, week_8, Questionable: knee, week_9, Questionable: knee, week_12, Questionable: knee, week_13, Probable: knee, week_14, Questionable: knee, week_15, Questionable: knee, week_16, Probable: knee, week_17'] [['Tommy Kelly'], 'Questionable: knee, week_6, Questionable: knee, week_7, Questionable: knee, week_8, Questionable: knee, week_9, Injured Reserve: knee, week_11, Injured Reserve: knee, week_12, Injured Reserve: knee, week_13, Injured Reserve: knee, week_14, Injured Reserve: knee, week_15, Injured Reserve: knee, week_16, Injured Reserve: knee, week_17, Injured Reserve: knee, week_19, Injured Reserve: knee, week_20'] [['Jerod Mayo'], 'Questionable: ankle, week_4, Questionable: ankle, week_5, Probable: ankle, week_6, Injured Reserve: shoulder, week_7, Injured Reserve: shoulder, week_8, Injured Reserve: shoulder, week_9, Injured Reserve: shoulder, week_11, Injured Reserve: shoulder, week_12, Injured Reserve: shoulder, week_13, Injured Reserve: shoulder, week_14, Injured Reserve: shoulder, week_15, Injured Reserve: shoulder, week_16, Injured Reserve: shoulder, week_17, Injured Reserve: shoulder, week_19, Injured Reserve: shoulder, week_20'] [['Devin McCourty'], 'Questionable: shoulder, week_7, Probable: shoulder, week_8, Questionable: head, week_17'] [['T.J. Moe'], 'Injured Reserve: Achilles, week_1, Injured Reserve: Achilles, week_2, Injured Reserve: Achilles, week_3, Injured Reserve: Achilles, week_4, Injured Reserve: Achilles, week_5, Injured Reserve: Achilles, week_6, Injured Reserve: Achilles, week_7, Injured Reserve: Achilles, week_8, Injured Reserve: Achilles, week_9, Injured Reserve: Achilles, week_11, Injured Reserve: Achilles, week_12, Injured Reserve: Achilles, week_13, Injured Reserve: Achilles, week_14, Injured Reserve: Achilles, week_15, Injured Reserve: Achilles, week_16, Injured Reserve: Achilles, week_17, Injured Reserve: Achilles, week_19, Injured Reserve: Achilles, week_20'] [['Rob Ninkovich'], 'Probable: groin, week_6, Probable: groin, week_7, Probable: groin, week_8, Questionable: foot, week_11, Questionable: ankle, week_17'] [['Stevan Ridley'], 'Probable: shoulder, week_2, Questionable: knee, week_5, Questionable: knee, week_6'] [['Matt Slater'], 'Questionable: knee, week_2, Out: wrist, week_3, Out: wrist, week_4, Out: wrist, week_5, Out: wrist, week_6, Questionable: wrist, week_8, Probable: wrist, week_9, Probable: wrist, week_11, Probable: wrist, week_12, Probable: wrist, week_13, Probable: right shoulder, week_14, Probable: wrist, week_15'] [['Nate Solder'], 'Probable: back, week_7, Questionable: concussion, week_15, Questionable: concussion, week_16, Questionable: concussion, week_17'] [['Brandon Spikes'], 'Questionable: knee, week_12, Probable: knee, week_13, Questionable: knee, week_14, Questionable: knee, week_15, Questionable: knee, week_16, Questionable: knee, week_17'] [['Zach Sudfeld'], 'Questionable: hamstring, week_2, Probable: hamstring, week_3, Probable: hamstring, week_4, Questionable: hamstring, week_5'] [['Will Svitek'], 'Questionable: knee, week_1, Questionable: knee, week_2, Questionable: knee, week_3, Questionable: knee, week_4, Questionable: knee, week_5, Questionable: ankle, week_14, Questionable: ankle, week_15, Questionable: ankle, week_16, Questionable: ankle, week_17'] [['Aqib Talib'], 'Questionable: hip, week_6, Questionable: hip, week_7, Questionable: hip, week_8, Questionable: hip, week_9, Questionable: hip, week_11, Questionable: hip, week_12, Questionable: hip, week_13, Questionable: hip, week_14, Questionable: hip, week_15, Questionable: hip, week_16, Probable: hip, week_17'] [['Kenbrell Thompkins'], 'Questionable: shoulder, week_5, Questionable: hip, week_14, Questionable: hip, week_15, Questionable: hip, week_16, Questionable: hip, week_17'] [['Shane Vereen'], 'Out: wrist, week_2, Injured Reserve: wrist, week_3, Injured Reserve: wrist, week_4, Injured Reserve: wrist, week_5, Injured Reserve: wrist, week_6, Injured Reserve: wrist, week_7, Injured Reserve: wrist, week_8, Injured Reserve: wrist, week_9, Injured Reserve: wrist, week_11, Probable: wrist, week_12, Probable: wrist, week_13, Probable: wrist, week_14, Probable: wrist, week_15, Questionable: groin, week_16, Probable: groin, week_17'] [['Sebastian Vollmer'], 'Questionable: foot, week_4, Questionable: foot, week_5, Injured Reserve: leg, week_9, Injured Reserve: leg, week_11, Injured Reserve: leg, week_12, Injured Reserve: leg, week_13, Injured Reserve: leg, week_14, Injured Reserve: leg, week_15, Injured Reserve: leg, week_16, Injured Reserve: leg, week_17, Injured Reserve: leg, week_19, Injured Reserve: leg, week_20'] [['Leon Washington'], 'Questionable: thigh, week_2, Questionable: thigh, week_3, Questionable: thigh, week_4, Questionable: thigh, week_5, Questionable: ankle, week_6, Questionable: ankle, week_7, Questionable: ankle, week_8, Questionable: ankle, week_9, Questionable: ankle, week_11, Questionable: ankle, week_12'] [['Ryan Wendell'], 'Questionable: concussion, week_6'] [['Chris White'], 'Questionable: back, week_13'] [['Vince Wilfork'], 'Probable: foot, week_4, Out: Achilles, week_5, Injured Reserve: Achilles, week_6, Injured Reserve: Achilles, week_7, Injured Reserve: Achilles, week_8, Injured Reserve: Achilles, week_9, Injured Reserve: Achilles, week_11, Injured Reserve: Achilles, week_12, Injured Reserve: Achilles, week_13, Injured Reserve: Achilles, week_14, Injured Reserve: Achilles, week_15, Injured Reserve: Achilles, week_16, Injured Reserve: Achilles, week_17, Injured Reserve: Achilles, week_19, Injured Reserve: Achilles, week_20'] [['Adrian Wilson'], 'Injured Reserve: hamstring, week_1, Injured Reserve: hamstring, week_2, Injured Reserve: hamstring, week_3, Injured Reserve: hamstring, week_4, Injured Reserve: hamstring, week_5, Injured Reserve: hamstring, week_6, Injured Reserve: hamstring, week_7, Injured Reserve: hamstring, week_8, Injured Reserve: hamstring, week_9, Injured Reserve: hamstring, week_11, Injured Reserve: hamstring, week_12, Injured Reserve: hamstring, week_13, Injured Reserve: hamstring, week_14, Injured Reserve: hamstring, week_15, Injured Reserve: hamstring, week_16, Injured Reserve: hamstring, week_17, Injured Reserve: hamstring, week_19, Injured Reserve: hamstring, week_20'] [['Tavon Wilson'], 'Questionable: hamstring, week_5, Questionable: hamstring, week_6, Questionable: hamstring, week_7, Questionable: hamstring, week_8, Questionable: hamstring, week_9'] [['Markus Zusevics'], 'Injured Reserve: undisclosed, week_1, Injured Reserve: undisclosed, week_2, Injured Reserve: undisclosed, week_3, Injured Reserve: undisclosed, week_4, Injured Reserve: undisclosed, week_5, Injured Reserve: undisclosed, week_6, Injured Reserve: undisclosed, week_7, Injured Reserve: undisclosed, week_8, Injured Reserve: undisclosed, week_9, Injured Reserve: undisclosed, week_11, Injured Reserve: undisclosed, week_12, Injured Reserve: undisclosed, week_13, Injured Reserve: undisclosed, week_14, Injured Reserve: undisclosed, week_15, Injured Reserve: undisclosed, week_16, Injured Reserve: undisclosed, week_17, Injured Reserve: undisclosed, week_19, Injured Reserve: undisclosed, week_20'] )选择器将父#标记元素与select元素选择器以后代组合定位,以获取所有子option标记元素。

option

Dim nodeList As Object, i As Long Set nodeList = HTMLDoc.querySelectorAll("#pkCodEstado option") For i = 0 To nodeList.Length-1 Debug.Print nodeList.item(i).innerText Next 已在顶部声明,因此您实际上不需要再次声明。您应该在所有模块的顶部使用i,然后声明所有变量。您的代码中有许多未声明的变量。