I will try to be as explicit as possible.
I am trying to extract the value of the current dollar and put it in a label, searching the Internet I found several codes that do similar things but use the class label as a filter, which does not work for me because I am trying to get information from a table, I don't understand much html and how it is composed, this is the code that I have so far for data extraction.
Private Sub cargarPagina()
web = New WebBrowser() ' Se instancia el WebBrower
web.ScriptErrorsSuppressed = True ' Oculta la ventana de errores si algún script de la página falló (de todas formas no los necesitamos)
web.Navigate(New Uri("http://www.bna.com.ar/Personas")) ' Carga la página web creando un nuevo documento HTML
' Este Handler permite continuar con el proceso una vez que se ha cargado TODA la página (ya que si no ha cargado lanzará un error)
AddHandler web.DocumentCompleted, New WebBrowserDocumentCompletedEventHandler(AddressOf cargarValorDolar)
End Sub
Private Sub cargarValorDolar()
If (web.Document IsNot Nothing) Then ' En caso de que la página no halla cargado bien el documento
Dim divs = web.Document.Body.GetElementsByTagName("td") ' Obtiene todos los elementos <div> de la página web
For Each div As HtmlElement In divs ' Recorre la lista de elementos <div>
Dim className As String = div.GetAttribute("className") ' Obtiene el atributo [class] (nos servirá de filtro)
If className = "tit" Then ' El texto con el que se compara es el que muestra el valor del dólar en pesos argentinos
MsgBox(div.InnerText) ' Se obtiene el valor del dólar
End If
Next
End If
End Sub
I managed to extract information but not what I need
the page is
The information I want to get is the current dollar today for the purchase
This is the page code
<div class="tab-pane fade in active" id="billetes">
<table class="table cotizacion">
<thead>
<tr>
<th class="fechaCot">21/2/2019</th>
<th>Compra</th>
<th>Venta</th>
</tr>
</thead>
<tbody>
<tr>
<td class="tit">Dolar U.S.A</td>
<td>38,6000</td> <---- Este es el dato que quiero extraer.
<td>40,4000</td>
</tr>
<tr>
<td class="tit">Euro</td>
<td>45,0000</td>
<td>47,0000</td>
</tr>
<tr>
<td class="tit">Real *</td>
<td>980,0000</td>
<td>1080,0000</td>
</tr>
</tbody>
</table>
<a href="#" class="link-cotizacion" data-toggle="modal" data-target="#modalHistorico" id="buttonHistoricoBilletes">Ver histórico</a>
<div class="legal">Hora Actualización: 15:06</div>
<div class="legal">(*) cotización cada 100 unidades.</div>
</div>
The code that I have is a code that I found in another post and I tried to adapt it to mine but I only manage to extract irrelevant information for my purpose.
Thank you very much in advance.
I have created a form with the controls
WebBrowser1
,Button1
andLabel1
. UsingHtmlAgilityPack
, it is as simple as this:wbSource
= Gets and stores the source code previously loaded into the controlWebBrowser
.document
= Load the previously obtained source codehtmlBody
= Loop through the code to the indicated nodehtmlValues
= Stores the attributeInnerText
obtained after the previous parsing withhtmlBody
//table[@class='table cotizacion']//tr//td[2]
: Looks up the classtable cotizacion
of any tag<table>
, accesses the first tag<tr>
it finds, and retrieves the value of the second tag<td>
, which turns out to be the expected value.