2015-11-28 59 views
1

我正在編寫一些VBA腳本,可以從某些網站獲取航班價格的值。但我的代碼沒有得到最低的價格,它得到一個隨機的價格...如何使用VBA在div標籤內的類中檢索值?

我需要檢索下面的代碼中的一些值。

我需要檢索第一個 U $。 我試過使用

cia = doc.getElementsByClassName("currency ng-binding")(0).innerText 

但它沒有工作。

我的代碼:

<div class="box-filter cias" ng-init="opened = false"> 
       <h3>Cias. Aéreas</h3> 
       <span>a partir de:</span> 
       <div ng-class="(opened || hasFilter(AvailabilityResult.FilterOptions.AirCompanies)) ? 'opened' : 'wrapper-cias'" id="wrapper-cia" class="opened"> 
        <label> 
         <input type="checkbox" class="cias-aerea chkFilterAirCompany filterControl" value="All" ng-checked="!hasFilter(AvailabilityResult.FilterOptions.AirCompanies)" ng-disabled="!hasFilter(AvailabilityResult.FilterOptions.AirCompanies)" ng-click="SetFilter($event, 'all', 'Co');"> 
         <strong class="label-option full">Todas as Companhias</strong> 
        </label> 

        <!-- ngRepeat: airCompany in AvailabilityResult.FilterOptions.AirCompanies --><label ng-repeat="airCompany in AvailabilityResult.FilterOptions.AirCompanies" ng-show="airCompany.MinPrice > 0 || airCompany.MinPoint > 0" class="ng-scope"> 
         <input type="checkbox" class="cias-aerea chkFilterAirCompany filterControl ng-valid ng-dirty ng-valid-parse ng-touched" ng-model="airCompany.Checked" value="true" ng-click="SetFilter($event, airCompany.CiaCode, 'Co');"> 
         <strong class="label-option ng-binding">Aeroméxico</strong> 
         <span class="price ng-binding" ng-show="!UsePoints"><span class="currency ng-binding">US$</span>**298**</span> 
         <!-- ngIf: UsePoints --> 
        </label><!-- end ngRepeat: airCompany in AvailabilityResult.FilterOptions.AirCompanies --><label ng-repeat="airCompany in AvailabilityResult.FilterOptions.AirCompanies" ng-show="airCompany.MinPrice > 0 || airCompany.MinPoint > 0" class="ng-scope"> 
         <input type="checkbox" class="cias-aerea chkFilterAirCompany filterControl ng-pristine ng-untouched ng-valid" ng-model="airCompany.Checked" value="false" ng-click="SetFilter($event, airCompany.CiaCode, 'Co');"> 
         <strong class="label-option ng-binding">Tam</strong> 
         <span class="price ng-binding" ng-show="!UsePoints"><span class="currency ng-binding">US$</span>549</span> 
         <!-- ngIf: UsePoints --> 
        </label><!-- end ngRepeat: airCompany in AvailabilityResult.FilterOptions.AirCompanies --><label ng-repeat="airCompany in AvailabilityResult.FilterOptions.AirCompanies" ng-show="airCompany.MinPrice > 0 || airCompany.MinPoint > 0" class="ng-scope"> 
         <input type="checkbox" class="cias-aerea chkFilterAirCompany filterControl ng-pristine ng-untouched ng-valid" ng-model="airCompany.Checked" value="false" ng-click="SetFilter($event, airCompany.CiaCode, 'Co');"> 
         <strong class="label-option ng-binding">Delta </strong> 
         <span class="price ng-binding" ng-show="!UsePoints"><span class="currency ng-binding">US$</span>907</span> 
         <!-- ngIf: UsePoints --> 
        </label><!-- end ngRepeat: airCompany in AvailabilityResult.FilterOptions.AirCompanies --><label ng-repeat="airCompany in AvailabilityResult.FilterOptions.AirCompanies" ng-show="airCompany.MinPrice > 0 || airCompany.MinPoint > 0" class="ng-scope"> 
         <input type="checkbox" class="cias-aerea chkFilterAirCompany filterControl ng-pristine ng-untouched ng-valid" ng-model="airCompany.Checked" value="false" ng-click="SetFilter($event, airCompany.CiaCode, 'Co');"> 
         <strong class="label-option ng-binding">Copa </strong> 
         <span class="price ng-binding" ng-show="!UsePoints"><span class="currency ng-binding">US$</span>1.028</span> 
         <!-- ngIf: UsePoints --> 
        </label><!-- end ngRepeat: airCompany in AvailabilityResult.FilterOptions.AirCompanies --><label ng-repeat="airCompany in AvailabilityResult.FilterOptions.AirCompanies" ng-show="airCompany.MinPrice > 0 || airCompany.MinPoint > 0" class="ng-scope"> 
         <input type="checkbox" class="cias-aerea chkFilterAirCompany filterControl ng-pristine ng-untouched ng-valid" ng-model="airCompany.Checked" value="false" ng-click="SetFilter($event, airCompany.CiaCode, 'Co');"> 
         <strong class="label-option ng-binding">American Airlines</strong> 
         <span class="price ng-binding" ng-show="!UsePoints"><span class="currency ng-binding">US$</span>1.180</span> 
         <!-- ngIf: UsePoints --> 
        </label><!-- end ngRepeat: airCompany in AvailabilityResult.FilterOptions.AirCompanies --><label ng-repeat="airCompany in AvailabilityResult.FilterOptions.AirCompanies" ng-show="airCompany.MinPrice > 0 || airCompany.MinPoint > 0" class="ng-scope"> 
         <input type="checkbox" class="cias-aerea chkFilterAirCompany filterControl ng-pristine ng-untouched ng-valid" ng-model="airCompany.Checked" value="false" ng-click="SetFilter($event, airCompany.CiaCode, 'Co');"> 
         <strong class="label-option ng-binding">Gol</strong> 
         <span class="price ng-binding" ng-show="!UsePoints"><span class="currency ng-binding">US$</span>1.278</span> 
         <!-- ngIf: UsePoints --> 
        </label><!-- end ngRepeat: airCompany in AvailabilityResult.FilterOptions.AirCompanies --><label ng-repeat="airCompany in AvailabilityResult.FilterOptions.AirCompanies" ng-show="airCompany.MinPrice > 0 || airCompany.MinPoint > 0" class="ng-scope"> 
         <input type="checkbox" class="cias-aerea chkFilterAirCompany filterControl ng-pristine ng-untouched ng-valid" ng-model="airCompany.Checked" value="false" ng-click="SetFilter($event, airCompany.CiaCode, 'Co');"> 
         <strong class="label-option ng-binding">LAN</strong> 
         <span class="price ng-binding" ng-show="!UsePoints"><span class="currency ng-binding">US$</span>1.985</span> 
         <!-- ngIf: UsePoints --> 
        </label><!-- end ngRepeat: airCompany in AvailabilityResult.FilterOptions.AirCompanies --> 

       </div> 
       <a href="javascript:void(0);" ng-click="opened = !opened" ng-hide="hasFilter(AvailabilityResult.FilterOptions.AirCompanies) || AvailabilityResult.FilterOptions.AirCompanies.length <= 3" id="buttonViewAllCias" class="ver-todas ng-binding ng-hide">Ver todas as cias</a 
      </div> 
+0

不知道爲什麼第一個是不是最低的,如果他們總是有序。您也可以評估所有7個結果並返回最低值(循環數組,字典等...) –

+1

如果使用類「price」而不是「currency ng-binding」,該怎麼辦? – barrowc

回答

1

試圖通過一步步走得到你所需要的。您需要的價格是text-node,它位於price-span的內部,它位於label的內部,其位於wrapper-cia div的內部。

所以先得到div然後得到這div這是一個label的第二個孩子。然後獲得span作爲label的第三個孩子,最後得到text-node作爲span的第二個孩子。 HTH

' Add reference to Microsoft Internet Controls (SHDocVw) 
' Add reference to Microsoft HTML Object Library 

Dim ie As SHDocVw.InternetExplorer 
Dim doc As MSHTML.HTMLDocument 
Dim url As String 

url = "file:///C:/Temp/ValueInsideDiv.html" 
Set ie = New SHDocVw.InternetExplorer 
ie.Visible = True 
ie.navigate url 

While ie.Busy Or ie.readyState <> READYSTATE_COMPLETE: DoEvents: Wend 

Set doc = ie.document 

Dim wrapperCia As MSHTML.HTMLDivElement 
Set wrapperCia = doc.getElementById("wrapper-cia") 

Dim ciaLabels As MSHTML.IHTMLElementCollection ' first has index 0, second 1 etc. 
Set ciaLabels = wrapperCia.Children 

Dim priceLabel As MSHTML.HTMLLabelElement 
Set priceLabel = ciaLabels(1) 

Dim priceLabelChildren As MSHTML.IHTMLElementCollection 
Set priceLabelChildren = priceLabel.Children 

Dim priceSpan As MSHTML.HTMLSpanElement 
Set priceSpan = priceLabelChildren(2) 

Dim priceNode As MSHTML.IHTMLDOMNode 
Set priceNode = priceSpan.ChildNodes(1) 

Dim price As String 
price = priceNode.NodeValue 

Debug.Print "Price = " & price 

ie.Quit 
Set ie = Nothing 

輸出

Price = **298** 

或者,如果你想這樣做一點點簡單的(但更容易出錯),然後就像這樣:

doc.getElementsByClassName("currency ng-binding")(0).NextSibling.NodeValue 
1

由於格式良好的HTML成爲符合要求的有效XML,因此您可以轉換原始文檔,然後使用XPath query,這需要對HTML文件進行輕微的清理以形成良好的格式。

具體而言,除去重音é,並用適當的實體替換><&gt;&lt;和正確地與/>關閉<input>節點將呈現一個有效的XML。當然,具有較大的html文件更多的清理可能需要,但低於使用貼出的內容:

Sub ConvertAndParseWellFormedHTML() 

    Dim FileNum As Integer 
    Dim DataLine As String, htmlfile As String 

    ' ADD VBA REFERENCE: MICROSOFT XML, v3.0 or v6.0 ' 
    Dim oXMLFile As New MSXML2.DOMDocument 
    Dim ResultList As MSXML2.IXMLDOMNodeList 

    ' READ HTML FILE LINE BY LINE, CLEANING OUT PROBLEM XML VALUES ' 
    FileNum = FreeFile() 
    Open "C:\Path\To\Locally\Saved.html" For Input As #FileNum 

    htmlfile = "" 
    While Not EOF(FileNum) 
     Line Input #FileNum, DataLine 
     htmlfile = htmlfile + DataLine + vbNewLine 

     htmlfile = Replace(Replace(Replace(htmlfile, " <=", "&lt;"), " > ", "&gt;"), ";"">", ";""/>") 
     htmlfile = Replace(Replace(htmlfile, "é", "e"), "</a", "</a>") 
    Wend 

    ' LOAD XML CONTENT AS STRING ' 
    oXMLFile.LoadXML htmlfile 

    ' EXTRACT NODES INTO LIST AND REWRITE NODES ' 
    ' (CHANGE NUMBER 1 FOR NODE POSITION TO GET OTHER PRICES) ' 
    Set ResultList = oXMLFile.SelectNodes("/div/div/label[1]/span") 

    Debug.Print ResultList(0).Text   

    Set ResultList = Nothing 
    Set oXMLFile = Nothing 
End Sub 

輸出

US$**298** 
相關問題