Results 1 to 11 of 11

Thread: [RESOLVED] (Another) Get Table with WebBrowser

  1. #1

    Thread Starter
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,145

    Resolved [RESOLVED] (Another) Get Table with WebBrowser

    I built a simple webbrowser program, and now I want to download a specific table (League stats) on a specific website (http://www.nfl.com/standings?category=league). I can save the entire page and then using copy-paste, put it into an Excel spreadsheet...however, I would like to avoid those manual steps. I have looked at several postings on how to get table data, but have failed in all attempts to make any of them work. Here is a pic of my webbrowser on the page which includes the table I want...(at the bottom of the snapshot). Can anyone assist? Thanks in advance.

    Attachment 93231

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: (Another) Get Table with WebBrowser

    this will paste into a single column, should be easy enough to split it into multiple, but out of time now

    Code:
    For Each ele In wb.document.all 'getelementsbytagname("Table")
        If ele.innertext = "Conf" Then
            
            clipboard.Clear
            clipboard.SetText ele.offsetparent.offsetparent.innertext
            Exit For
        End If
    
    Range("a1").PasteSpecial
    where wb is an instance of IE or a shell window
    Last edited by westconn1; Nov 15th, 2012 at 03:07 AM.
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,145

    Re: (Another) Get Table with WebBrowser

    What I have tried is to get a string of the entire 'text' of the html file with this code:

    Code:
    dim myText as string
    myText = frmMain.WebBrowser1.Document.documentElement.innerHTML
    It APPEARS that the entire file is not dumped into my string variable as when I search the text, I do not find any <table> tags
    I thought it might be that the page is in frames, so I tried this:


    Code:
    Dim i As Integer
    
    For i = 0 To frmMain.WebBrowser1.Document.frames.length - 1   
       myText = myText & WebBrowser1.Document.frames(i).Document.documentElement.innerHTML
    Next i
    but I got this error: "Runtime Error Access is Denied." So I assumed there are no frames on the page.

    I CAN get the entire page dumped to a .html web page and then can open it with Word and see the table, or with Notepad and see the <table> tags. So this led me to believe my initial dump into myText did not get everything.

    I do that with this command: WebBrowser1.ExecWB OLECMDID_SAVEAS, OLECMDEXECOPT_DODEFAULT
    I guess I could use Shell to open the file I saved (html format) and then put that file into a string and THEN extract the data after the appropriate <table> tag....but that would be cumbersome, and a lot of extra coding to put that data into an excel spreadsheet and/or Access DB table.

  4. #4

    Thread Starter
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,145

    Re: (Another) Get Table with WebBrowser

    Thx Westconn....JUST saw your post...I will try later...thanks....

  5. #5
    New Member
    Join Date
    Nov 2012
    Posts
    11

    Re: (Another) Get Table with WebBrowser

    Not sure how regular you want to do this but I simply used "inspect element" in Chrome copied the table as HTML (from the Elements tab) and pasted into excel. It pasted column by column and row by row.

    Just thought I would mention this. Apologies if it is off track.

  6. #6
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: (Another) Get Table with WebBrowser

    i just edited the code i posted as when rushing out this morning i left in a couple of things that should have been removed, that was because i wrote and tested in VBA where there is no clipboard object

    for whatever reason the table you want does not appear to be in a table element
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  7. #7

    Thread Starter
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,145

    Re: (Another) Get Table with WebBrowser

    bazz.....thanks...I'll try that at home (can't load Chrome on work comp)....It's a weekly thing during the NFL season...so if that works, that will suffice. HOWEVER, am still working on the VB webbrowser approach...who knows, I may eventually figure that one out.

    If Westconn1 can't figure out why that 'table' doesn't appear as a table element (post #6), who am I???? :-)

  8. #8
    New Member
    Join Date
    Nov 2012
    Posts
    11

    Re: (Another) Get Table with WebBrowser

    I am sure Westconn1 will get there. I am sure any browser that offers debugging addons will do. Most of the major browsers will do I am sure. I mainly use Chrome.

  9. #9
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: (Another) Get Table with WebBrowser

    try like
    Code:
    tabel = ""
    For Each ele In wb.document.all
        If ele.innertext = "Conf" Then
            Set tms = ele.offsetparent.offsetparent.getelementsbytagname("tr")
            For rw = 0 To tms.Length - 1
                Set mytm = tms(rw).getelementsbytagname("td")
                For col = 0 To mytm.Length - 1
                    tabel = tabel & " " & mytm(col).innertext & vbTab
                Next
                tabel = tabel & vbNewLine
            Next
            
     
             clipboard.Clear
             clipboard.SetText CStr(tabel)
            Exit For
        End If
    Next
    Range("a1").PasteSpecial
    appears to give the desired result
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  10. #10

    Thread Starter
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,145

    Re: (Another) Get Table with WebBrowser

    HUAH!!! I will ....thanks.....

  11. #11

    Thread Starter
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,145

    Re: (Another) Get Table with WebBrowser

    Westconn (Pete?)....fantastic! DOES work like a charm....thanks.

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