1

Estou a tentar pegar o valor dentro de uma <table>, pois precisava de obter o valor dentro de um <tr> que está dentro da table.

Já tentei de algumas formas, mas a que estou a usar, dá-me valor null. De facto acabei por apos algumas tentativas consegui mais ou menos o que queria mas com este codigo ele lista dos os td como posso separar eles de facto estou a tentar tirar os valores de longitude e latidude

Esta Foi a parte que eu alterei

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

Este e o codigo na pagina Eu procuro uma maneira de extrair so a longitude e latitude somente. Country:Portugal State/Region:Faro District Latitude: 37.2887925  (37° 17′ 19.65″ N) Longitude: -8.5930041  (8° 35′ 34.81″ W)

Este e o meu código:

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 Respostas 2

0

Para gerir páginas WEB em pode utilizar uma biblioteca fantástica, HTML Agility Pack.

Com este pack pode obter informação específica de forma muito fácil recorrendo a XPath (tutorial aqui: XPath Tutorial da W3 Schools), que no seu caso seria algo do género:

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

De facto consegui obter os resultados desejados em vb.net e sem o html agility pack.

Aqui esta o codigo final

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

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .