Foro de elhacker.net

Programación => .NET (C#, VB.NET, ASP) => Mensaje iniciado por: Crazy.sx en 22 Octubre 2014, 22:46 pm



Título: Extraer un dato de un formulario de una página web con VB.NET
Publicado por: Crazy.sx en 22 Octubre 2014, 22:46 pm
Hola, buenas tardes. Me gustaría saber cómo puedo extraer de un formulario un dato de las cajas de texto que hay para luego ponerlo en un textbox de mi aplicación.

En el código fuente de la página web aparece algo como esto:

Código
  1. <input name="codigo2" type="text" size="16" value="10409" maxlength="6" onKeyPress="return soloNumero2(event)" disabled>

En el campo código del formulario sólo se ve el número 10409 y además no se puede editar. Bueno, es ese número el que quisiera trasladarlo hacia un textbox de la aplicación que intento hacer.

Sería algo como:

Código
  1. Me.Textbox1.Text = Codigo_Obtenido_desde_Pagina_WEB

Espero que se haya entendido lo que pretendo y me pueda orientar.

Saludos.


Título: Re: Extraer un dato de un formulario de una página web con VB.NET
Publicado por: Eleкtro en 23 Octubre 2014, 00:14 am
Puedes hacerlo con una simple expresión regular:

Código
  1. Imports System.Text.RegularExpressions
  2.  
  3. Public Class Form1
  4.  
  5.    Private ReadOnly htmlSource As String =
  6. <a><![CDATA[
  7. <!DOCTYPE html>
  8. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  9.    <body>
  10.  
  11.        <div class="test">
  12.       <input name="codigo2" type="text" size="16" value="10409" maxlength="6" onKeyPress="return soloNumero2(event)" disabled>
  13.       <input name="codigo3" type="text" size="16" value="10410" maxlength="6" onKeyPress="return soloNumero3(event)" disabled>
  14.        </div>
  15.  
  16.    </body>
  17. </html>
  18. ]]></a>.Value
  19.  
  20.    Private Sub Test() Handles MyBase.Shown
  21.  
  22.        Dim regEx As New Regex(<a><![CDATA[value=\"(\d+)\"]]></a>.Value,
  23.                               RegexOptions.Compiled Or RegexOptions.Multiline Or RegexOptions.IgnoreCase)
  24.  
  25.        If regEx.IsMatch(htmlSource) Then
  26.  
  27.            For Each match As Match In regEx.Matches(htmlSource)
  28.  
  29.                For Each capture As Capture In match.Groups(1).Captures
  30.  
  31.                    MessageBox.Show(capture.Value)
  32.  
  33.                Next capture
  34.  
  35.            Next match
  36.  
  37.        Else
  38.            MessageBox.Show("No coincide la búsqueda")
  39.  
  40.        End If
  41.  
  42.    End Sub
  43.  
  44. End Class



...Pero las expresiones regulares no se diseñaron con la intención de parsear un html así que voy a mostrarte una solución más óptima, utilizando la librería HtmlAgilityPack.

Código
  1.    Public Class Form1
  2.  
  3.    Private ReadOnly htmlSource As String =
  4. <a><![CDATA[
  5.    <!DOCTYPE html>
  6.    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  7.       <body>
  8.            <div class="test">
  9.                <input name="codigo2" type="text" size="16" value="10409" maxlength="6" onKeyPress="return soloNumero2(event)" disabled>
  10.                <input name="codigo3" type="text" size="16" value="10410" maxlength="6" onKeyPress="return soloNumero3(event)" disabled>
  11.            </div>
  12.       </body>
  13.    </html>
  14.    ]]></a>.Value
  15.  
  16.    Private htmlDoc As New HtmlAgilityPack.HtmlDocument
  17.    Private htmlNodes As HtmlAgilityPack.HtmlNodeCollection
  18.  
  19.    Private Sub Test() Handles MyBase.Shown
  20.  
  21.        ' Load the html document.
  22.        htmlDoc.LoadHtml(htmlSource)
  23.  
  24.        ' Select the nodes.
  25.        htmlNodes = htmlDoc.DocumentNode.SelectNodes("//div[@class='test']/input")
  26.  
  27.        ' Loop trough the nodes.
  28.        For Each node As HtmlAgilityPack.HtmlNode In htmlnodes
  29.  
  30.            Dim Value As Integer
  31.            ' Get the "value" attribute.
  32.            Integer.TryParse(node.GetAttributeValue("value", -1I), Value)
  33.  
  34.            MessageBox.Show(CStr(Value))
  35.  
  36.        Next node
  37.  
  38.    End Sub
  39.  
  40. End Class

Saludos