尝试从网站获取一些数据 [英] Try to get some data from a web site
问题描述
我想从金融网站获得不同的价格,并将其存储在Excel文件中.我没有足够的知识,我想知道我从网站获取数据的代码是最好的还是可以做得更好.网站的HTML代码就是这一段.我想在"Prezzo di chiusura"之后获得最后的td,其内部为103,74.
I would like to get a different prices from financial site and store them in an Excel file. I don't have good knowledge and I would like to know if the code I did to get data from a web site is the best or it could be done better. The web site HTML code is this one. I would like to get the last td at the end just after 'Prezzo di chiusura' that has inside 103,74.
我有以下问题:
- 我使用了get elementsbytagname("td")(39),我刚刚计算了页面中的td编号,是否有更好的方法来解决该td问题?
- 我注意到有时我有以前的价格,而不是网页上看到的价格,用我的代码我可以访问与网页上看到的区域数据不同的区域数据,直到不更新为止,我会看到仅以前的数据?
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代码是这样的:
The Excel VBA code is this one:
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
推荐答案
CSS选择器:
您可以使用以下CSS选择器: table:last-child .table_label〜td
You can use a CSS selector of: table:last-child .table_label ~ td
带有 td
标记的元素,紧随其后的是具有className .table_label
的 table
标记.
The element with td
tag following last table
tag with className .table_label
.
CSS查询:
VBA:
您可以通过 document
的 querySelector
方法应用CSS选择器.
You apply the CSS selector with the querySelector
method of document
.
Debug.Print Objie.Document.querySelector("table:last-child .table_label ~ td").innerText
这篇关于尝试从网站获取一些数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!