+ Reply to Thread
Results 1 to 12 of 12

Inner text data from web page

Hybrid View

  1. #1
    Registered User
    Join Date
    06-25-2013
    Location
    Minnesota
    MS-Off Ver
    Excel 2010
    Posts
    22

    Inner text data from web page

    I am having trouble grabbing the inner text (4.00% and 4.25%) from a web pages source code... Any help would be greatly appriciated...

    Source Code:
    HTML Code: 
    VBA Coding:
        Set Pelements = HTMLdoc.getElementsByTagName("P")
        
        Worksheets("WebData").Range("N2:N41").ClearContents
        
        r = 0
        For Each Pelement In Pelements
            If Pelement.Style = "text-align: center;" Then
                Range("N2").Offset(r, 0).Value = Pelement.innerText
                r = r + 1
            End If
        Next

  2. #2
    Forum Contributor
    Join Date
    09-29-2011
    Location
    Kolkata, India
    MS-Off Ver
    Excel 2003/2007
    Posts
    182

    Re: Inner text data from web page

    Hi,

    What output you are getting currently and if possible let us know the web site address.

    Regards
    taps

  3. #3
    Registered User
    Join Date
    06-25-2013
    Location
    Minnesota
    MS-Off Ver
    Excel 2010
    Posts
    22

    Re: Inner text data from web page

    Thanks for the response...

    I'm getting no output and the link is..
    https://www.fanniemae.com/singlefami...e-modification

  4. #4
    Forum Contributor
    Join Date
    09-29-2011
    Location
    Kolkata, India
    MS-Off Ver
    Excel 2003/2007
    Posts
    182

    Re: Inner text data from web page

    Hi Smith3rs,

    I think the code you have provide is partial code. However, try this one, here small modification has done...

    
       Set Pelements = HTMLdoc.getElementsByTagName("P")
        
        Worksheets("WebData").Range("N2:N41").ClearContents
        
        r = 0
        For Each Pelement In Pelements
            If Pelement(r).Style = "text-align: center;" Then
                Range("N2").Offset(r, 0).Value = Pelement(r).innerText
                r = r + 1
            End If
        Next
    regards
    taps

  5. #5
    Registered User
    Join Date
    06-25-2013
    Location
    Minnesota
    MS-Off Ver
    Excel 2010
    Posts
    22

    Re: Inner text data from web page

    Hey Taps - Here is the full code.. maybe you can tailor your code to fit in it.. thanks

    Sub Extract_FS()
    
    '   Fannie Mae Modification Fixed Interest Rate
    
    '   Runs, but not pulling any data
    
        Dim URL As String
        Dim IE As InternetExplorer
        Dim HTMLdoc As HTMLDocument
        Dim Pelements As IHTMLElementCollection
        Dim Pelement As HTMLTableCell
        Dim r As Long
    
    '   Goes to and nagivates web page
        
        URL = "https://www.fanniemae.com/singlefamily/fannie-mae-modification"
        
        Set IE = New InternetExplorer
        
        With IE
            .Navigate URL
            .Visible = False
        
            While .Busy Or .ReadyState <> READYSTATE_COMPLETE: DoEvents: Wend
        
            Set HTMLdoc = .Document
        End With
        
    '   Allows website to catch up and fully load
        
        Application.Wait Now + TimeValue("00:00:01")
          
    '   Searches source code for data
    '   Imports to "WebData" sheet
    
       Set Pelements = HTMLdoc.getElementsByTagName("P")
        
        Worksheets("WebData").Range("N2:N41").ClearContents
        
        r = 0
        For Each Pelement In Pelements
            If Pelement(r).Style = "text-align: center;" Then
                Range("N2").Offset(r, 0).Value = Pelement(r).innerText
                r = r + 1
            End If
        Next
                
    End Sub

  6. #6
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Inner text data from web page

    Another way - this will be much faster:
    Sub Extract_FS()
        Dim d
        Dim p As Long
        With CreateObject("winhttp.winhttprequest.5.1")
            .Open "GET", "https://www.fanniemae.com/singlefamily/fannie-mae-modification", False
            .Send
            d = Split(.responseText, "<p style=""text-align: center;"">")
            For p = 1 To UBound(d)
                Worksheets("WebData").Range("N2").Offset(p, 0).Value = Split(d(p), "<")(0)
            Next p
        End With
    End Sub

  7. #7
    Registered User
    Join Date
    06-25-2013
    Location
    Minnesota
    MS-Off Ver
    Excel 2010
    Posts
    22

    Re: Inner text data from web page

    Hey Kyle -

    Wow that worked out great! Thank you very much sir.

  8. #8
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Inner text data from web page

    No problem

    Tanks for the feedback

  9. #9
    Forum Contributor
    Join Date
    09-29-2011
    Location
    Kolkata, India
    MS-Off Ver
    Excel 2003/2007
    Posts
    182

    Re: Inner text data from web page

    Hi Kyle123,

    Your code is fantastic

    Hi Smith3rs,

    Although Kyle's code is working fine, I am also attaching a code which is modified version of your code.
    It is working fine in my computer


    Sub Extract_FS_Final()
    
    
        Dim URL As String
        Dim HTMLdoc As HTMLDocument
        Dim Pelements As IHTMLElementCollection
        Dim Pelement As HTMLTableCell
        Dim r As Long
    
        
        URL = "https://www.fanniemae.com/singlefamily/fannie-mae-modification"
        
        Set IE = CreateObject("InternetExplorer.Application")
    
        IE.Visible = 0
        IE.navigate URL
        state = 0
        
        Do Until state = 4
            DoEvents
            state = IE.readyState
        Loop
        Set HTMLdoc = IE.document
        
        Application.Wait Now + TimeValue("00:00:01")
          
       Set Pelements = HTMLdoc.getElementsByTagName("P")
        
        Worksheets("WebData").Range("A2:A41").ClearContents
        
        r = 0
        For Each Pelement In Pelements
            If Pelement.Style.textAlign = "center" Then
                Range("A2").Offset(r, 0).Value = Pelement.innerText
                r = r + 1
            End If
        Next
              
        MsgBox "Done"
        
    End Sub
    regards
    taps

  10. #10
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Inner text data from web page

    Thanks

    Try to avoid using Internet Explorer unless you really need to, it's slow and nasty. If you are wanting to use the document object model as above, consider:
    Sub Extract_FS()
    
        Dim oDom As Object: Set oDom = CreateObject("htmlFile")
        Dim oNodes As Object
        Dim p
        Dim l As Long
        
        With CreateObject("winhttp.winhttprequest.5.1")
            .Open "GET", "https://www.fanniemae.com/singlefamily/fannie-mae-modification", False
            .Send
            oDom.body.innerHtml = .responseText
        End With
        
        Set oNodes = oDom.getElementsByTagName("p")
        
        For Each p In oNodes
            If p.Style.textAlign = "center" Then
                ActiveSheet.Range("A2").Offset(l, 0).Value = p.innerText
                l = l + 1
            End If
        Next p
        
        
    End Sub

  11. #11
    Forum Contributor
    Join Date
    09-29-2011
    Location
    Kolkata, India
    MS-Off Ver
    Excel 2003/2007
    Posts
    182

    Re: Inner text data from web page

    Thanks a ton

    Regards
    taps

  12. #12
    Registered User
    Join Date
    06-25-2013
    Location
    Minnesota
    MS-Off Ver
    Excel 2010
    Posts
    22

    Re: Inner text data from web page

    You guys are awesome!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1