As the value within a text / td

1

I'm trying to get the value inside a <table> because I needed to get the value inside a <tr> that is inside the table.

I've tried it in some ways, but the one I'm using gives me null value. In fact I ended up after a few attempts I got more or less what I wanted but with this code it lists the td how can i separate them in fact I'm trying to take the values of longitude and latidude

This was the part that 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 only.          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
    
asked by anonymous 26.08.2018 / 16:15

2 answers

0

I actually got 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
    
28.08.2018 / 09:41
0

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

With this pack you can get specific information very easily using XPath (tutorial here: XPath Tutorial from W3 Schools ), which in your case would be something like this:

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
    
27.08.2018 / 11:40