Results 1 to 9 of 9

Thread: [RESOLVED] Extracting HTML Table Data using DOM

  1. #1

    Thread Starter
    Member
    Join Date
    Dec 2002
    Posts
    56

    Resolved [RESOLVED] Extracting HTML Table Data using DOM

    I am try to create a program that would extract song title, album name from AOL Music Page. Here is the link.

    http://music.aol.com/artist/coldplay/435023/video

    Data is given in a table. I have been trying to do it with "For Each table In wb.Document.body.getElementsByTagName("table")" code. But the problem is it gives some error. Dont worry about the error, the basic problem is I am not at all familiar with extracting data from tables. I know how to do it from normal pages using .innerhtml, .innertext etc but not from tables.

    Can someone provide me a sample code to extract data from the above mentioned page?
    Last edited by Inman; May 10th, 2006 at 09:40 AM.

  2. #2

    Thread Starter
    Member
    Join Date
    Dec 2002
    Posts
    56

    Re: Extracting HTML Table Data using DOM

    Never mind, I figured it out. If anyone needs to know, just reply.

  3. #3
    Frenzied Member
    Join Date
    Apr 2005
    Posts
    1,907

    Arrow Re: Extracting HTML Table Data using DOM

    Quote Originally Posted by Inman
    Never mind, I figured it out. If anyone needs to know, just reply.
    Inman i am intrested in knowing how that can be done.

  4. #4

    Thread Starter
    Member
    Join Date
    Dec 2002
    Posts
    56

    Re: [RESOLVED] Extracting HTML Table Data using DOM

    It is very simple. You can call it just like an Excel control.

    VB Code:
    1. Dim testarray(10)
    2. Dim ObjElement As HTMLObjectElement
    3. Set ObjElement = Form1.WebBrowser1.Document.All().tags("table").Item(0)
    4.  
    5. For i = 0 To ObjElement.rows.length - 1
    6. testarray(i) = ObjElement.rows(i).cells(0).innerText
    7. Next i

    What happens is the contents of first table in the page are copied to the array.

  5. #5
    Frenzied Member
    Join Date
    Apr 2005
    Posts
    1,907

    Re: [RESOLVED] Extracting HTML Table Data using DOM

    many thanks for u reply. could u tell me what refrences and what controles do i need to run this code and where the output will be shown.Thanks

  6. #6

    Thread Starter
    Member
    Join Date
    Dec 2002
    Posts
    56

    Re: [RESOLVED] Extracting HTML Table Data using DOM

    You need to add a referece to Microsoft HTML Object Library.

    In the above code, the data is stored into testarray. Since only the row variable is being looped, only the 1st column of the table will be stored into the array. i.e.

    testarray(0) = ObjElement.rows(0).cells(0).innerText [ i.e. (row 1, column 1)]
    testarray(1) = ObjElement.rows(1).cells(0).innerText [ i.e. (row 2, column 1)]

    If you want 2nd column's value,

    testarray(0) = ObjElement.rows(0).cells(1).innerText [ i.e. (row 1, column 2)]

    Like that it works.

  7. #7
    Frenzied Member
    Join Date
    Apr 2005
    Posts
    1,907

    Re: [RESOLVED] Extracting HTML Table Data using DOM

    Quote Originally Posted by Inman
    You need to add a referece to Microsoft HTML Object Library.

    In the above code, the data is stored into testarray. Since only the row variable is being looped, only the 1st column of the table will be stored into the array. i.e.

    testarray(0) = ObjElement.rows(0).cells(0).innerText [ i.e. (row 1, column 1)]
    testarray(1) = ObjElement.rows(1).cells(0).innerText [ i.e. (row 2, column 1)]

    If you want 2nd column's value,

    testarray(0) = ObjElement.rows(0).cells(1).innerText [ i.e. (row 1, column 2)]

    Like that it works.
    Thank u for u reply. So u mean i need a webbrowser controle with a button . How i can display the result in a listview ? It would be helpf full if u upload a sample project since i want to learn to do the same for another music website by extracting artistname,albumname,songname ,... So from your code i see u do most of the things without parsing wich makes life easier!

  8. #8

    Thread Starter
    Member
    Join Date
    Dec 2002
    Posts
    56

    Re: [RESOLVED] Extracting HTML Table Data using DOM

    Okay. Consider you have a webpage which has a table. You have a listview with corresponding number of columns as of webpage table called lv. To copy all contents of the table to lv.

    VB Code:
    1. Dim ObjElement As HTMLObjectElement
    2. Set ObjElement = Form1.WebBrowser1.Document.All().tags("table").Item(0)
    3.  
    4. lv.listitems.clear
    5. itmc=0
    6.  
    7. For i = 0 To ObjElement.rows.length - 1
    8. itmc=itmc+1
    9. lv.listitems.add itmc , , ObjElement.rows(i).cells(0).innerText
    10. For j = 1 To ObjElement.cols.length - 1
    11. lv.listitems(itmc).listsubitems.add , , ObjElement.rows(i).cells(j).innerText
    12. next j
    13. Next i

  9. #9
    Frenzied Member
    Join Date
    Apr 2005
    Posts
    1,907

    Re: [RESOLVED] Extracting HTML Table Data using DOM

    Inmant thank u for u nice explanation. Could u look at this post of mine . I have some difficulty getting data from html.Thanks
    http://www.vbforums.com/showthread.php?t=404705

Posting Permissions

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



Click Here to Expand Forum to Full Width