VS 2010 How to get data from html source in visual basic as string-VBForums
Results 1 to 9 of 9

Thread: How to get data from html source in visual basic as string

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2015
    Posts
    16

    How to get data from html source in visual basic as string

    trying to read the customer number from there into a string

    <TD width="45%" valign=center align=right class=HeaderLabel2>John Doe(123456789)&nbsp;[]</TD>


    here is also the problem that if youve get the value of Headerlabel2 it must be split up like name, forename, costumerid...

    buttoncontrol and fill in form that i can but i cant find a way to read the costumer number in a string in visual basic 2010

    someone knows the code?

    thinking of some kind code like this but cant find it

    Try
    Dim username As String
    Dim password As String
    techid = My.Settings.username
    techpass = My.Settings.password
    browser.Document.GetElementById("username_5").InnerText = username
    browser.Document.GetElementById("password_5").InnerText = password
    browser.Document.GetElementById("btnsubmit_6").InvokeMember("Click")
    Catch ex As Exception
    End Try
    Last edited by blackeagle8100; Mar 2nd, 2015 at 11:44 AM. Reason: privacy

  2. #2

    Thread Starter
    Junior Member
    Join Date
    Jan 2015
    Posts
    16

    Re: How to get data from html source in visual basic as string

    i need to get the source code out of the webpage into vb so i can split it up in multiple strings.
    Last edited by blackeagle8100; Jul 29th, 2015 at 05:29 PM.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jan 2015
    Posts
    16

    Re: How to get data from html source in visual basic as string

    no solution?

  4. #4
    Lively Member ShadowTzu's Avatar
    Join Date
    Oct 2014
    Location
    France
    Posts
    65

    Re: How to get data from html source in visual basic as string

    vb.net Code:
    1. Dim splitted() As String = Split(SOURCE_STRING, "HeaderLabel2>")
    2.  
    3. Dim data As String = Trim(Split(splitted(1), "<")(0))
    4. Dim split_data() As String = Split(data, " ")
    5.  
    6. Dim Forename As String = split_data(0)
    7. Dim Name As String = split_data(1)
    8.  
    9. 'clean customer
    10. Dim temp_customer As String = Split(Trim(split_data(2)), ")")(0)
    11. Dim Customer_ID As String = temp_customer.Substring(1)
    12.  
    13. MsgBox(Forename & vbCrLf & Name & vbCrLf & Customer_ID)

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Jan 2015
    Posts
    16

    Re: How to get data from html source in visual basic as string

    Quote Originally Posted by ShadowTzu View Post
    vb.net Code:
    1. Dim splitted() As String = Split(SOURCE_STRING, "HeaderLabel2>")
    2.  
    3. Dim data As String = Trim(Split(splitted(1), "<")(0))
    4. Dim split_data() As String = Split(data, " ")
    5.  
    6. Dim Forename As String = split_data(0)
    7. Dim Name As String = split_data(1)
    8.  
    9. 'clean customer
    10. Dim temp_customer As String = Split(Trim(split_data(2)), ")")(0)
    11. Dim Customer_ID As String = temp_customer.Substring(1)
    12.  
    13. MsgBox(Forename & vbCrLf & Name & vbCrLf & Customer_ID)


    can you please explaine the code a bit because i tried copy paste the code but it doesnt work


    got a error on Dim data As String = Trim(Split(splitted(1), "<")(0))
    index was inside the bounds of the array...

    thx in advanced

  6. #6
    Lively Member ShadowTzu's Avatar
    Join Date
    Oct 2014
    Location
    France
    Posts
    65

    Re: How to get data from html source in visual basic as string

    vb.net Code:
    1. 'you need to replace SOURCE_STRING by the source code of your webpage example: Dim splitted() As String = Split(GetCurrentWebDoc.documentElement.outerHTML, "HeaderLabel2>")
    2. Dim splitted() As String = Split(SOURCE_STRING, "HeaderLabel2>")
    3. 'from here splitted is an array of string with two entry: splitted(0) = every text found before "HeaderLabel2>" and  splitted(1) = every text found after  "HeaderLabel2>"
    4. 'if Split doesn't found "HeaderLabel2>" then splitted contain only one entry: splitted(0) = GetCurrentWebDoc.documentElement.outerHTML -> The following code will not work.
    5.  
    6. 'you can add a new line to check that:
    7. If splitted.Count = 1 then
    8.     MsgBox ("Data can't be found!")
    9.     Return
    10. end If
    11.  
    12. 'From here splitted contain: splitted(1) = "Deceuninck Rik (645956140)&nbsp;[Mediors]</TD> <!-- DESIGN END ADD HEADERJSP:ICS_WFM--> ...........(and the rest of the source)"
    13.  
    14. 'We need to get every text before the first "<"
    15. Dim data As String = Trim(Split(splitted(1), "<")(0))
    16.  
    17. 'result: data = "Deceuninck Rik (645956140)&nbsp;[Mediors]"
    18.  
    19. 'Split again with space char:
    20. Dim split_data() As String = Split(data, " ")
    21.  
    22. 'result: split_data(0) and split_data(1) contain what you need and split_data(2) need to be cleaned a little
    23.  
    24. Dim Forename As String = split_data(0)
    25. Dim Name As String = split_data(1)
    26.      
    27. 'clean customer because split_data(2) = "(645956140)&nbsp;[Mediors]"
    28. Dim temp_customer As String = Split(Trim(split_data(2)), ")")(0)
    29. Dim Customer_ID As String = temp_customer.Substring(1)
    30.      
    31. MsgBox(Forename & vbCrLf & Name & vbCrLf & Customer_ID)

  7. #7
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    4,948

    Re: How to get data from html source in visual basic as string

    ShadowTzu i assume you come from a vb6 background? Loads of legacy code there.

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Jan 2015
    Posts
    16

    Re: How to get data from html source in visual basic as string

    So if i follow your structure a bit and clean up the code a bit :-) i came to this "solution"


    Dim splitted() As String = Split(SOURCE, "align=right>")
    SOURCE = splitted(1)
    txtinput.Text = SOURCE
    Dim data() As String = Split(SOURCE, "&nbsp")
    SOURCE = data(0)
    txtinput.Text = SOURCE

    this gives the result

    Deceuninck Rik (645956140)


    then i can resplit on the space so that
    split(0) = name
    split(1) = forename
    split(2) = costumer number

    but if the value of source is more like 1 name it splits wrong...

    example
    van den berghe john (123456789)

    then it splits wrong
    split(0) = van
    split(1) = den
    split(2) = berghe

    so this gives a false value

    what im now trying to do is find the numeric string and import it as costumer number but didnt find a solution jet to split the name and forname properly :-)


    thx in advanced
    Blackeagle8100

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Jan 2015
    Posts
    16

    Re: How to get data from html source in visual basic as string

    txtinput.Text = ""
    klantennummer = ""
    ShowPageSource()
    Dim splitted() As String = Split(SOURCE, "align=right>")
    SOURCE = splitted(1)
    txtinput.Text = SOURCE
    Dim data() As String = Split(SOURCE, "&nbsp")
    SOURCE = data(0)
    Dim myChars() As Char = SOURCE.ToCharArray()
    For Each ch As Char In myChars
    If Char.IsDigit(ch) Then
    klantennummer = klantennummer & ch
    End If
    Next

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.