現在表標題設置爲中提取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>
你試過'tblNameArr =陣列(.Document。 getElementById(「a1」)。innerText,「現金流量」,「標題3」,「標題4」)?我剛剛替換了數組的第一個元素,但您明白了。 **編輯:**對不起,但id a1不包含您正在查找的表格標題,對不對? **編輯2 **你想要這個嗎? 重要財務指標' – nhee
我也試過tblNameArr = Array(.Document.getElementById(「a1」)。innerText,但它不起作用是的,「重要財務指標」正是我想要的用作表格標題,但其標籤名稱包含太多的屬性,所以我不知道如何獲得innerText。 –
當我嘗試'str = ie.document.getElementsByTagName(「body」)(0).innerText'我(重要財務指標)將被表示爲??????我不知道如何獲得您的語言中的字符 – Hubvill