2015-04-30 34 views
1

現在表標題設置爲中提取HTML標記文本,並把它作爲表題

tblNameArr = Array("Balance Sheet", "Cash Flow", "Header 3", "Header 4") 

如何根據html標記文本改變四個冠軍的名字嗎?

例如我想將第一個表名改爲「重要財務指標」,其名稱爲「a1」,id也爲「a1」,並將下表的名稱改爲「資產負債表」,「現金流量表」和「綜合損益表「相應?

請參考下面的html代碼。

Sub GetFinanceData() 
For x = 1 To 10 
Dim URL As String, elemCollection As Object 
Dim t As Integer, r As Integer, c As Integer 

Worksheets("Stocks").Select 
Worksheets("Stocks").Activate 

'Open IE and Go to the Website 

URL = "http://stock.finance.sina.com.cn/hkstock/finance/00001.html" 
URL = Cells(x, 1) 

Set IE = CreateObject("InternetExplorer.Application") 
With IE 
    .navigate URL 
    .Visible = True 

    Do While .Busy = True Or .readyState <> 4 
     Loop 
    DoEvents 

Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = _ 
ThisWorkbook.Worksheets("Stocks").Range("B" & x).Value  'You could even simplify it and just state the name as Cells(x,2) 


'Select the Report Type 

Set selectItems = IE.Document.getElementsByTagName("select") 
    For Each i In selectItems 
     i.Value = "zero" 
     i.FireEvent ("onchange") 
     Application.Wait (Now + TimeValue("0:00:05")) 
    Next i 

    Do While .Busy: DoEvents: Loop 

ActiveSheet.Range("A1:K500").ClearContents 

ActiveSheet.Range("A1").Value = .Document.getElementsByTagName("h1")(0).innerText 
ActiveSheet.Range("B1").Value = .Document.getElementsByTagName("em")(0).innerText 

'Find and Get Table Data 

tblNameArr = Array("Balance Sheet", "Cash Flow", "Header 3", "Header 4") 
tblStartRow = 5 
Set elemCollection = .Document.getElementsByTagName("TABLE") 
For t = 0 To elemCollection.Length - 1 
    For r = 0 To (elemCollection(t).Rows.Length - 1) 
     For c = 0 To (elemCollection(t).Rows(r).Cells.Length - 1) 
     ActiveSheet.Cells(r + tblStartRow, c + 1) = elemCollection(t).Rows(r).Cells(c).innerText 
    Next c 
Next r 

ActiveSheet.Cells(r + tblStartRow + 2, 1) = tblNameArr(t) 
tblStartRow = tblStartRow + r + 4 

Next t 

End With 

' cleaning up memory 

IE.Quit 

Next x 


End Sub 

下面是HTML代碼:

<!--重要財務指標 start--> 
<a name="a1" id="a1"></a> 
<div class="part02"> 
    <div class="sub01"> 
     <div class="sub01_tt fblue"> 
      <span class=" selected"><a href="#a1" target="_self">重要財務指標</a></span> 
      <span class=""><a href="#a2" target="_self">資產負債表</a></span> 
      <span class=""><a href="#a3" target="_self">現金流量表</a></span> 
      <span class=""><a href="#a4" target="_self">綜合損益表</a></span> 
      <em class="rt">報表類型:<select class="fgrey" style="width:100px;" interface="getFinanceStandardForjs?symbol=$symbol&financeStanderd=" table="tableGetFinanceStandard" onchange="selectData(this);"> 
      <option value="all" >全部</option> 
      <option value="zero" >年報</option> 
      <option value="1" >中報</option> 
      <option value="2" >一季報</option> 
      <option value="3" >三季報</option> 
     </select></em> 
    </div> 
+0

你試過'tblNameArr =陣列(.Document。 getElementById(「a1」)。innerText,「現金流量」,「標題3」,「標題4」)?我剛剛替換了數組的第一個元素,但您明白了。 **編輯:**對不起,但id a1不包含您正在查找的表格標題,對不對? **編輯2 **你想要這個嗎? 重要財務指標' – nhee

+0

我也試過tblNameArr = Array(.Document.getElementById(「a1」)。innerText,但它不起作用是的,「重要財務指標」正是我想要的用作表格標題,但其標籤名稱包含太多的屬性,所以我不知道如何獲得innerText。 –

+0

當我嘗試'str = ie.document.getElementsByTagName(「body」)(0).innerText'我(重要財務指標)將被表示爲??????我不知道如何獲得您的語言中的字符 – Hubvill

回答

0

其由標籤<a name="a1" id="a1"></a>創建的元件是空的。它只是一個鏈接錨點。它不包含某些東西。所以得到這個元素是沒用的。

一種方法可能是,運行在所有A元素,並挑選那些指href="#a1"href="#a2" ...

例子:

... 

nameBalanceSheet = "Balance Sheet" 
nameCashFlow = "Cash Flow" 
nameHeader3 = "Header 3" 
nameHeader4 = "Header 4" 

Set elemCollection = .Document.getElementsByTagName("A") 
For i = 0 To elemCollection.Length - 1 
If Right(elemCollection(i).href, 3) = "#a1" Then 
    nameBalanceSheet = elemCollection(i).innerText 
ElseIf Right(elemCollection(i).href, 3) = "#a2" Then 
    nameCashFlow = elemCollection(i).innerText 
ElseIf Right(elemCollection(i).href, 3) = "#a3" Then 
    nameHeader3 = elemCollection(i).innerText 
ElseIf Right(elemCollection(i).href, 3) = "#a4" Then 
    nameHeader4 = elemCollection(i).innerText 
End If 
Next 

tblNameArr = Array(nameBalanceSheet, nameCashFlow, nameHeader3, nameHeader4) 

... 
+0

它完美的作品!謝謝阿克塞爾!請你也看看我的新問題?謝謝〜鏈接: http://stackoverflow.com/questions/3 0010792 /自動下載-Excel的文件JavaScript的ASPX,網頁 –

相關問題