+ Reply to Thread
Results 1 to 6 of 6

How would I grab the source code from a website and place it into a text file using vba?

Hybrid View

111StepsAhead How would I grab the source... 06-14-2012, 09:55 AM
romperstomper Re: How would I grab the... 06-14-2012, 10:16 AM
Kyle123 Re: How would I grab the... 06-14-2012, 10:16 AM
111StepsAhead Re: How would I grab the... 06-14-2012, 10:23 AM
Kyle123 Re: How would I grab the... 06-14-2012, 10:25 AM
111StepsAhead Re: How would I grab the... 06-14-2012, 10:31 AM
  1. #1
    Forum Contributor
    Join Date
    04-11-2011
    Location
    Columbus, Ohio
    MS-Off Ver
    Excel 2007
    Posts
    325

    How would I grab the source code from a website and place it into a text file using vba?

    Essentially it would be a web scraper. I'm either looking for existing free code or a good place to learn how to make one.
    Last edited by 111StepsAhead; 06-14-2012 at 10:23 AM. Reason: Users posted 2 solutions to the problem.

  2. #2
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,099

    Re: How would I grab the source code from a website and place it into a text file using vb

    I'm sure there are many different ways, but this may help
    Sub testit()
        LogIESource "http://www.microsoft.com", "C:\microsoft.txt"
    End Sub
    Sub LogIESource(strURL As String, strFilePath As String)
        Dim objIE            As Object
        Dim  FileNumber as long
    
        Set objIE = CreateObject("InternetExplorer.Application")
        ' Get unused file
    
        objIE.Navigate strURL
        Do While objIE.ReadyState <> 4
            DoEvents
        Loop
        FileNumber = FreeFile
        ' Create log file
        Open strFilePath For Output As #FileNumber
    
        Print #FileNumber, objIE.Document.DocumentElement.outerhtml
    
        ' Close file.
        Close #FileNumber
    End Sub
    Everyone who confuses correlation and causation ends up dead.

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

    Re: How would I grab the source code from a website and place it into a text file using vb

    Typically you wouldn't use vba for this, however since you want t ouse VBA, see if this gets you started:
    Sub GetSource()
        With CreateObject("MSXML2.XMLHTTP")
            .Open "GET", "http://www.excelforum.com"
            .send
            Do: DoEvents: Loop Until .ReadyState = 4
            
            Open ThisWorkbook.Path & "\Output.txt" For Output As #1
                Print #1, .responsetext
            Close #1
            
        End With
    End Sub
    Webscraping can get very complex very quickly and as I said VBA isn't the ideal tool

  4. #4
    Forum Contributor
    Join Date
    04-11-2011
    Location
    Columbus, Ohio
    MS-Off Ver
    Excel 2007
    Posts
    325

    Re: How would I grab the source code from a website and place it into a text file using vb

    Thanks for the quick responses. Hope you guys didn't have to put too much effort into making that code. I am trying to expand on what I've learned over the last few months here and this seemed like a fun way to do that. Both of your guys code worked great.

    I will mark this thread as solved but as an extra question, what programming language would be better for webscraping?

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

    Re: How would I grab the source code from a website and place it into a text file using vb

    Probably Python

  6. #6
    Forum Contributor
    Join Date
    04-11-2011
    Location
    Columbus, Ohio
    MS-Off Ver
    Excel 2007
    Posts
    325

    Re: How would I grab the source code from a website and place it into a text file using vb

    Quote Originally Posted by Kyle123 View Post
    Probably Python
    I shall keep this in mind when I get some free time.

+ 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