As the value within a <td>text</td>

Asked

Viewed 168 times

1

I’m trying to get the value inside a <table>, because it needed to get the value within a <tr> that is inside the table.

I’ve tried it a few ways, but the one I’m using null. In fact I ended up after a few attempts I got more or less what I wanted but with this code he lists them all I’m actually trying to get the longitude and latidude values

This is the part I changed

Public Function getele()
    Dim bodytext As Windows.Forms.HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName("td")
    For Each Tag As HtmlElement In bodytext
        ListBox1.Items.Add(Tag.InnerText)
        'MessageBox.Show(Tag.InnerText)
        If Tag = Nothing Then
            MsgBox("")
        End If
    Next
End Function

This is the code on the page I look for a way to extract only longitude and latitude. Country:Portugal State/Region:Faro District Latitude: 37.2887925  (37° 17 19.65 N) Longitude: -8.5930041  (8° 35 34.81 W)

This is my code:

Imports System.Text.RegularExpressions
Imports System.Net
Imports System.IO
Public Class Form9


Dim Col As HtmlElementCollection
Dim Ele As HtmlElement
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    'GetExternalIP()

    WebBrowser1.ScriptErrorsSuppressed = True
    WebBrowser1.Navigate("https://whatismyipaddress.com/ip/2001:8a0:7594:5401:a9da:74f8:9ecb:38bb")
    WaitForPageLoad()
    getele()

    ListBox1.Items.Add(WebBrowser1.Document.GetElementById("table").InnerText())
End Sub

Private Shared Function GetExternalIP() As String

    Dim Response As String = String.Empty

    Try

        Dim myWebClient As New System.Net.WebClient
        Dim whatIsMyIp As String = "http://automation.whatismyip.com/n09230945.asp"
        Dim file As New System.IO.StreamReader(myWebClient.OpenRead(whatIsMyIp))

        Response = file.ReadToEnd()
        file.Close()
        file.Dispose()
        myWebClient.Dispose()

    Catch ex As Exception
        Response = "Could not confirm External IP Address" & vbCrLf & ex.Message.ToString
    End Try

    Return Response


End Function

Private Sub getViews()
    Try
        Dim version = FileVersionInfo.GetVersionInfo("c:\windows\system32\ieframe.dll")
        'Depending on the navigator version, google's server sends diffetent pages, so
        'Here Detect ie version
        If version.ProductVersion < "8" Then
            lb_views.Text = WebBrowser1.Document.GetElementById("th").FirstChild.InnerText
        Else
            lb_views.Text = WebBrowser1.Document.GetElementById("td").FirstChild.InnerText
        End If

    Catch ex As Exception
        MsgBox(ex.ToString)
        Application.Exit()
    End Try

End Sub


Private Property pageready As Boolean = False

Private Sub WaitForPageLoad()
    AddHandler WebBrowser1.DocumentCompleted, New WebBrowserDocumentCompletedEventHandler(AddressOf PageWaiter)
    While Not pageready
        Application.DoEvents()
    End While
    pageready = False
End Sub

Private Sub PageWaiter(ByVal sender As Object, ByVal e As WebBrowserDocumentCompletedEventArgs)
    If WebBrowser1.ReadyState = WebBrowserReadyState.Complete Then
        pageready = True
        RemoveHandler WebBrowser1.DocumentCompleted, New WebBrowserDocumentCompletedEventHandler(AddressOf PageWaiter)
    End If
End Sub
Public Function getele()
    Dim elementList As HtmlElementCollection

    Dim curElement As HtmlElement

    elementList = WebBrowser1.Document.GetElementsByTagName("a")

    For Each curElement In elementList

        'You can use the innerText attribute to locate the hyperlink

        If curElement.GetAttribute("innerText").Equals("Latitude:") Then
            lb_views.Text = curElement.ToString

            curElement.InvokeMember("click")

        End If



        'Or use the innerHtml attribute

        If curElement.GetAttribute("innerHtml").Contains("<th>Latitude:</th>") Then

            curElement.InvokeMember("click")
            lb_views2.Text = curElement.ToString
        End If

    Next
End Function
End Class

2 answers

0

To manage web pages on can use a fantastic library, HTML Agility Pack.

With this pack can obtain specific information very easily by using Xpath (tutorial here: Xpath W3 Schools Tutorial), that in your case it would be something like:

Dim doc As HtmlDocument = New HtmlDocument
doc.Load(YouTestHtmlFilePath)

'supomos que a "Latitude" está na coluna 1 e "Longitude" na coluna 2
Dim latitudes = doc.DocumentNode.SelectNodes("//table/tr/td[1]")
Dim longitudes = doc.DocumentNode.SelectNodes("//table/tr/td[2]")

For i = 1 To 10
    Console.WriteLine(String.Format(@"Latitude = {0}; Longitude = {1}", latitudes(i).InnerText, longitudes(i).InnerText))
Next

0


In fact I was able to get the desired results in Vb.net and without the html Agility pack.

Here is the final code

Imports System.Text.RegularExpressions
Imports System.Net
Imports System
Imports System.IO

Public Class Form11


Dim Col As HtmlElementCollection
Dim Ele As HtmlElement
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click


    WebBrowser1.ScriptErrorsSuppressed = True
    WebBrowser1.Navigate("https://www.blockchain.com/explorer")
    WaitForPageLoad()

    'getele()

End Sub

Private Property pageready As Boolean = False

Private Sub WaitForPageLoad()
    AddHandler WebBrowser1.DocumentCompleted, New WebBrowserDocumentCompletedEventHandler(AddressOf PageWaiter)
    While Not pageready
        Application.DoEvents()
    End While
    pageready = False
End Sub

Private Sub PageWaiter(ByVal sender As Object, ByVal e As WebBrowserDocumentCompletedEventArgs)
    If WebBrowser1.ReadyState = WebBrowserReadyState.Complete Then
        pageready = True
        RemoveHandler WebBrowser1.DocumentCompleted, New WebBrowserDocumentCompletedEventHandler(AddressOf PageWaiter)
    End If
End Sub

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
    Dim ArrayTag As New ArrayList
    For Each item As HtmlElement In Me.WebBrowser1.Document.GetElementsByTagName("tr")
        ArrayTag.Add(item.InnerText)

    Next
    ListBox1.Items.Add(ArrayTag(1))
    lb_views.Text = ArrayTag(1)

    Dim ArrayTag2 As New ArrayList
    For Each item As HtmlElement In Me.WebBrowser1.Document.GetElementsByTagName("tr")
        ArrayTag2.Add(item.InnerText)

    Next
    ListBox1.Items.Add(ArrayTag(2))
    lb_views2.Text = ArrayTag2(2)

    Dim ArrayTag3 As New ArrayList
    For Each item As HtmlElement In Me.WebBrowser1.Document.GetElementsByTagName("tr")
        ArrayTag3.Add(item.InnerText)

    Next
    ListBox1.Items.Add(ArrayTag(3))
    TextBox1.Text = ArrayTag3(3)

    Dim ArrayTag4 As New ArrayList
    For Each item As HtmlElement In Me.WebBrowser1.Document.GetElementsByTagName("tr")
        ArrayTag4.Add(item.InnerText)

    Next
    ListBox1.Items.Add(ArrayTag(4))
    TextBox2.Text = ArrayTag4(4)
    'My.Settings.externalip = TextBox1.Text
    'My.Settings.state = TextBox2.Text
    'My.Settings.longitude = lb_views.Text
    'My.Settings.latitude = lb_views2.Text
    'My.Settings.Save()
End Sub
End Class

Browser other questions tagged

You are not signed in. Login or sign up in order to post.