2013-07-16 72 views
0

我想僅提取HTML表格中行中最右側單元格的內部文本。這是HTML代碼的一小部分。的行中包含810個細胞和TR標籤保持811個TD標籤:從一個單元格中提取innerText

</tr><tr align="center" id="spt_inner_row_2"><td nowrap="nowrap" bgcolor="#EEEEEE" style="border-bottom: 1px solid white; border-right: 1px solid white"> 
&nbsp;300 - 305&nbsp; 
</td><td nowrap="nowrap" bgcolor="#EEEEEE" style="border-bottom: 1px solid white; border-right: 1px solid white"> 
&nbsp;300 - 305&nbsp; 
</td><td nowrap="nowrap" bgcolor="#EEEEEE" style="border-bottom: 1px solid white; border-right: 1px solid white"> 
&nbsp;300 - 305&nbsp; 
</td><td nowrap="nowrap" bgcolor="#EEEEEE" style="border-bottom: 1px solid white; border-right: 1px solid white"> 
&nbsp;300 - 305&nbsp; 

我目前使用成功地提取從每個單元中的活性片的列A中的數據並將其粘貼的代碼:

Sub GetData() 

    Dim URL As String 
    Dim IE As InternetExplorer 
    Dim HTMLdoc As HTMLDocument 
    Dim TDelements As IHTMLElementCollection 
    Dim TDelement As HTMLTableCell 
    Dim r As Long 

    'For login use 
    Dim LoginForm As HTMLFormElement 
    Dim UserNameInputBox As HTMLInputElement 
    Dim PasswordInputBox As HTMLInputElement 

    URL = "https://www.whatever.com" 

    Set IE = New InternetExplorer 

    With IE 
     .navigate URL 
     .Visible = True 

     'Wait for page to load 
     While .Busy Or .readyState <> READYSTATE_COMPLETE: DoEvents: Wend 

     Set HTMLdoc = .document 

      'Enter login info 
      Set LoginForm = HTMLdoc.forms(0) 

      'Username 
      Set UserNameInputBox = LoginForm.elements("username") 
      UserNameInputBox.Value = "username" 

      'Password 
      Set PasswordInputBox = LoginForm.elements("password") 
      PasswordInputBox.Value = "password" 

      'Get the form input button and click it 

      Set SignInButton = LoginForm.elements("doLogin") 
      SignInButton.Click 

      'Wait for the new page to load 

      Do While IE.readyState <> READYSTATE_COMPLETE Or IE.Busy: DoEvents: Loop 

     'Auto-navigate to start page, so we need to navigate once more 

     .navigate URL 

     Do While IE.readyState <> READYSTATE_COMPLETE Or IE.Busy: DoEvents: Loop 

     End With 


    'Specify how to recognize data to extract 
    Set TDelements = HTMLdoc.getElementById("spt_inner_row_2").getElementsByTagName("TD") 


    r = 0 

    For Each TDelement In TDelements 

     ActiveSheet.Range("A1").Offset(r, 0).Value = TDelement.innerText 

     r = r + 1 

    Next 

End Sub 

我真正需要的只是提取HTML表格行中的最後一個(最右邊)單元格。有什麼建議麼?

+0

請參閱本[**鏈接1 **](http://stackoverflow.com/questions/17643483/trying-從網頁獲取數據從一個VBA代碼但有時它工作等等/ 17666816#17666816),[** Link2 **](http://support.microsoft.com/kb/17666816/)/questions/15844342/pull-upside-downside-capture-ratio-from-morningstar-com/15853293#15853293)&[** Link3 **](http://stackoverflow.com/questions/15959008/import-web-數據在-Excel的使用-VBA/15962055#15962055) – Santosh

回答

0

IHTMLElementCollection有一個length財產和item財產。該item財產可以採取一個數字指標,而是從零開始,所以最後一個條目是在length - 1

Dim TDelements As IHTMLElementCollection 

Set TDelements = HTMLdoc.getElementById("spt_inner_row_2").getElementsByTagName("TD") 

With TDelements 
    MsgBox .Item(.Length - 1).InnerText 
End With 
相關問題