Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
402 views
in Technique[技术] by (71.8m points)

xml - VB.net Get text/string from html element

I'm having major trouble trying to get bits of elements and returning the strings. I have a few exmaples of trying to get the strings and what not but failing hard.

HTML Phrasing is difficult for me to do so help would be appreciated.

Explantion of what I need:

I need to get the strinsg of different elements off this site when entering a IP http://www.ip-tracker.org/

I need pretty much all the details but into labels or text boxes.

Or this with xml phrasing http://ip-api.com/xml/8.8.8.8

So here is the exmaple that i've used so far but haven't got far with it.

Exmaple 1:

Dim client As New WebClient
Dim ip As String
Dim city As String
Dim Region As String

Private Function GetIp()
    Try
        Dim Page As String = client.DownloadString("http://www.ip-tracker.org/locator/ip-lookup.php?ip=82.16.38.43/")
        ip = Page.Substring(Page.IndexOf("IP Address:") + 80)
        ip = ip.Substring(0, city.IndexOf(" </td") + 30)
        TextBox2.Text = ("IP Address: " + ip)
    Catch ex As Exception
        city = "Unable to lookup"
    End Try
    Return 0
End Function

To call it:

getViews()
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Try xml linq

Imports System.Xml
Imports System.Xml.Linq
Module Module1
    Dim url As String = "http://ip-api.com/xml/8.8.8.8"
    Sub Main()
        Dim query As XElement = XElement.Load(url)
        Dim status As String = query.Element("status").Value
        Dim country As String = query.Element("country").Value
        Dim region As String = query.Element("region").Value
        Dim regionName As String = query.Element("region").Value
        Dim city As String = query.Element("city").Value
        Dim zip As String = query.Element("zip").Value
        Dim lat As Double = query.Element("lat").Value
        Dim lon As Double = query.Element("lon").Value
        Dim timezone As String = query.Element("timezone").Value
        Dim isp As String = query.Element("isp").Value
        Dim org As String = query.Element("org").Value
        Dim _as As String = query.Element("as").Value
        Dim subQuery As String = query.Element("query").Value

    End Sub

End Module

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...