• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 292
  • Last Modified:

parse section of html file with visual basic .net

I have a long htm file and I'm trying to get a small section of it.  Below is part of the file.  I want to find and grab "this is what I want to grab, but the data can change".  The text before/after it will.  How can I do that?

<tr><td>&nbsp;</td></tr>
         <tr> <TD align="center" COLSPAN=6 style="background-color:#FFFFFF">
                  <div style="background-color:b9d2ff; font-weight:bolder; color:#980000; font-size: 15px">Additional My IP Information - My IP Location Lookup</div>
                </td>
              </tr>
              <tr><td>
                    <table>
                          <tr><td>&nbsp;</td></tr>
                          <tr>
                                <TD width="100%" align='left' valign='top' class="slova">
                                <!--IP Location Code to trace address begin -->
<b>My IP Country Name</b>:&nbsp;&nbsp;<font color='#980000'> United States</font>&nbsp;&nbsp;<img src='images/cflags/us.png'><br><strong>My IP Country Continent</strong>: <font color='#980000'>North America</font>
 <font color='#980000'>(NA)</font>
<br><strong>My IP Country Latitude</strong>: (38)
<br><strong>My IP Country Longitude</strong>: (-98)
<br><b>My IP Country Code</b>:&nbsp;&nbsp;USA&nbsp;&nbsp;(US)<br><br><strong>My IP Address Region</strong>: <font color='#980000'>IL</font>
<br><b>My IP Address City</b>:&nbsp;&nbsp; <font color='#980000'>Chicago</font><br><b>IP Postal Code (IP Zip Code)</b>:&nbsp;&nbsp; <font color='#980000'>63333</font><br /><b>IP Area Code</b>:&nbsp;&nbsp; <font color='#980000'>999</font><br /><b>My IP Address Latitude</b>: (31.71)<br><b>My IP Address Longtitude</b>: (-93.5016)<br><br><strong>My ISP (Internet Service Provider)</strong>:&nbsp;<font color='#980000'> this is what I want to grab, but the data can change</font><br />
<strong>My IP Address lookup</strong> for <b>166.233.222.3</b> show IP which belongs to:&nbsp;<font color='#980000'>more copmany info</font><br />
<b>My Time zone</b>: America/Chicago<br><b>My Local time</b>:<span id=localTime>America/Chicago</span><br></TD>
</TR>
0
chadmanvb
Asked:
chadmanvb
  • 2
  • 2
1 Solution
 
Robberbaron (robr)Commented:
you will need to have some consistent text,
Do you have control of the source HTML ? if so, I have used html comments as placeholders.

<br><strong>My ISP (Internet Service Provider)</strong>:&nbsp;<font color='#980000'> this is what I want to grab, but the data can change</font><br />
<strong>My IP Address lookup</strong> for <b>166.233.222.3</b> show IP which belongs to:&nbsp;<font

do the parts in bold always exist in the response ?
0
 
chadmanvbAuthor Commented:
Sorry, I cant control the source and have to work with what I have.  The bold should be the same every time.
0
 
Robberbaron (robr)Commented:
then you can grab the html file and search it for the bolded substrings.

that way you can isolate the desired text.

my test code attached.
        Dim Sep1 As String = "<strong>My ISP (Internet Service Provider)</strong>"
        Dim Sep2 As String = "<strong>My IP Address lookup</strong>"

        Dim usefulText As String = ""

        Dim fnd1 As Integer = InStr(testHTML, Sep1)
        Dim fnd2 As Integer = -1
        If fnd1 > 0 Then
            'we have found the first tag
            fnd2 = InStr(fnd1, testHTML, Sep2)

            If fnd2 > 0 Then
                'found useful text
                usefulText = testHTML.Substring(fnd1 + Sep1.Length, fnd2 - fnd1 - Sep1.Length - 1)
            End If
        End If

Open in new window

Imports System.Xml
Imports System.Xml.Linq

Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim testHTML As String = <![CDATA[
<tr><td>&nbsp;</td></tr>
         <tr> <TD align="center" COLSPAN=6 style="background-color:#FFFFFF">
                  <div style="background-color:b9d2ff; font-weight:bolder; color:#980000; font-size: 15px">Additional My IP Information - My IP Location Lookup</div>
                </td>
              </tr>
              <tr><td> 
                    <table> 
                          <tr><td>&nbsp;</td></tr>
                          <tr> 
                                <TD width="100%" align='left' valign='top' class="slova">
                                <!--IP Location Code to trace address begin -->
<b>My IP Country Name</b>:&nbsp;&nbsp;<font color='#980000'> United States</font>&nbsp;&nbsp;<img src='images/cflags/us.png'><br><strong>My IP Country Continent</strong>: <font color='#980000'>North America</font>
 <font color='#980000'>(NA)</font>
<br><strong>My IP Country Latitude</strong>: (38)
<br><strong>My IP Country Longitude</strong>: (-98)
<br><b>My IP Country Code</b>:&nbsp;&nbsp;USA&nbsp;&nbsp;(US)<br><br><strong>My IP Address Region</strong>: <font color='#980000'>IL</font>
<br><b>My IP Address City</b>:&nbsp;&nbsp; <font color='#980000'>Chicago</font><br><b>IP Postal Code (IP Zip Code)</b>:&nbsp;&nbsp; <font color='#980000'>63333</font><br /><b>IP Area Code</b>:&nbsp;&nbsp; <font color='#980000'>999</font><br /><b>My IP Address Latitude</b>: (31.71)<br><b>My IP Address Longtitude</b>: (-93.5016)<br><br><strong>My ISP (Internet Service Provider)</strong>:&nbsp;<font color='#980000'> this is what I want to grab, but the data can change</font><br />
<strong>My IP Address lookup</strong> for <b>166.233.222.3</b> show IP which belongs to:&nbsp;<font color='#980000'>more copmany info</font><br />
<b>My Time zone</b>: America/Chicago<br><b>My Local time</b>:<span id=localTime>America/Chicago</span><br></TD>
</TR>]]>.Value

        Dim Sep1 As String = "<strong>My ISP (Internet Service Provider)</strong>"
        Dim Sep2 As String = "<strong>My IP Address lookup</strong>"

        Dim usefulText As String = ""

        Dim fnd1 As Integer = InStr(testHTML, Sep1)
        Dim fnd2 As Integer = -1
        If fnd1 > 0 Then
            'we have found the first tag
            fnd2 = InStr(fnd1, testHTML, Sep2)

            If fnd2 > 0 Then
                'found useful text
                usefulText = testHTML.Substring(fnd1 + Sep1.Length, fnd2 - fnd1 - Sep1.Length - 1)
            End If
        End If

    End Sub
End Class

Open in new window

0
 
chadmanvbAuthor Commented:
Thanks so much!  you solution worked great.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now