我想从金融网站获得不同的价格并将其存储在Excel文件中。 我没有很好的知识,我想知道我从网站获取数据的代码是最好还是可以做得更好。 网站HTML代码就是这个。我希望在'Prezzo di chiusura'之后的最后一个td,在103,74内。
我有这些问题:
HTML代码为:
<div class="instruments_company_summary">
<table class="table-noborders">
<tr>
<td class="table_label"> </td>
<td>
<div class="floatdx" style="padding-bottom:10px">
<div class="floatsx">
<div class="standard-button">
<a href="/it/strumenti/grafico/IT0004785355">Grafico</a> </div>
</div>
<div class="floatsx">
<div class="standard-button">
<a href="http://www.eurotlx.com/product-details/IT0004785355.pdf" target="_blank">Scheda</a> </div>
</div>
<div class="floatsx">
<div class="standard-button">
<a href="/it/strumenti/dettaglio/csv/IT0004785355">Scarica book</a> </div>
</div>
</div>
</td>
</tr>
<tr>
<td class="table_label">Isin</td>
<td>
<div class="floatsx" style="padding-top:4px;">IT0004785355</div>
</td>
</tr>
<tr>
<td class="table_label">Descrizione</td>
<td>Bpvi 7% 29dc16</td>
</tr>
<tr>
<td class="table_label">Prezzi aggiornati al</td>
<td>09-11-2015 21:28:48</td>
</tr>
</table>
<table>
<tr>
<th colspan="2">Book di negoziazione</th>
</tr>
<tr>
<td class="table_label">Var</td>
<td>0,05%</td>
</tr>
<tr>
<td class="table_label" style="border:0">Book a 5 livelli</td>
<td style="border:0; padding: 10px 0 5px">
<table>
<thead>
<tr>
<th>Q.tà Acquisto</th>
<th>Prezzo Acquisto</th>
<th>Prezzo Vendita</th>
<th>Q.tà Vendita</th>
</tr>
</thead>
<tbody>
<tr>
<td>-</td>
<td>-</td>
<td>-</td>
<td>-</td>
</tr>
<tr>
<td>-</td>
<td>-</td>
<td>-</td>
<td>-</td>
</tr>
<tr>
<td>-</td>
<td>-</td>
<td>-</td>
<td>-</td>
</tr>
<tr>
<td>-</td>
<td>-</td>
<td>-</td>
<td>-</td>
</tr>
<tr>
<td>-</td>
<td>-</td>
<td>-</td>
<td>-</td>
</tr>
</tbody>
</table>
</td>
</tr>
</table>
<table>
<tr>
<th colspan="2">Dati ultimo contratto</th>
</tr>
<tr>
<td class="table_label">Prezzo</td>
<td>103,93</td>
</tr>
<tr>
<td class="table_label">Quantità</td>
<td>5.000</td>
</tr>
<tr>
<td class="table_label">Data e ora</td>
<td>09-11-2015 16:59:33</td>
</tr>
</table>
<table>
<tr>
<th colspan="2">Dati giornalieri</th>
</tr>
<tr>
<td class="table_label">Prezzo di chiusura</td>
<td>103,74</td>
</tr>
Excel VBA代码就是这个:
Dim W As Worksheet: Set W = ActiveSheet
Dim Objie As Object
Dim xObj
Set Objie = CreateObject("InternetExplorer.Application")
Objie.Visible = False
Objie.Navigate "http://www.eurotlx.com/it/strumenti/dettaglio/IT0004785355"
While (Objie.Busy Or Objie.ReadyState <> 4)
DoEvents
Wend
Set xObj = Objie.Document.getElementsByTagName("td")(39)
W.Range("I3" ) = xObj.innerText
Set xObj = Nothing
Objie.Quit
Set Objie = Nothing
答案 0 :(得分:0)
这样会更好(使您不必更改以前表格中的行数)
Dim tbl, xObj
Set tbl = Objie.Document.getElementsByTagName("table")(3) '4th table on page
Set xObj = tbl.getElementsByTagName("td")(2) 'second td in that table
答案 1 :(得分:0)
您还可以搜索所需数据(“PREZZO DI CHIUSURA”)并使用nextElementSibling
属性获取给定值。这样,您可以始终确保获得的值与eurotlx.com提供的表结构无关。
Sub Scrape()
Dim ie As Object
'Get rid of cached sites
Shell "RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 255"
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = False
ie.navigate "http://www.eurotlx.com/it/strumenti/dettaglio/IT0004785355"
While (ie.Busy Or ie.ReadyState <> 4)
DoEvents
Wend
Set tds = ie.document.getElementsByClassName("table_label")
For Each TDelement In ie.document.getElementsByClassName("table_label")
If InStr(TDelement.innerText, "Prezzo di chiusura") Then
Range("I3") = TDelement.nextElementSibling.innerText
Exit For
End If
Next
End Sub
当然,这种方法的缺点是,如果站点管理员更改了“prezzo di chiusura”的innerText,Excel将无法找到它的值。
答案 2 :(得分:0)