Results 1 to 9 of 9

Thread: Grabbing contents off a webpage.

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Posts
    65

    Grabbing contents off a webpage.

    Hello there.

    I was wondering if anyone could provide me with any help on grabbing data from a webpage in VB 6, and looking for particular elements, and showing them elements only, ie a date. I've had a look for a few guides and tutorials although nothing substantial which shows me how to get the content. Also is there anyway how to refresh the data every few minutes?

    - Kind Regards
    Adam

  2. #2
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: Grabbing contents off a webpage.

    This is what I use to get HTML source. Just dump all this code into a module or something:

    VB Code:
    1. Option Explicit
    2. Public Declare Function InternetOpen Lib "wininet.dll" Alias "InternetOpenA" (ByVal sAgent As String, ByVal lAccessType As Long, ByVal sProxyName As String, ByVal sProxyBypass As String, ByVal lFlags As Long) As Long
    3. Public Declare Function InternetOpenUrl Lib "wininet.dll" Alias "InternetOpenUrlA" (ByVal hInternetSession As Long, ByVal sURL As String, ByVal sHeaders As String, ByVal lHeadersLength As Long, ByVal lFlags As Long, ByVal lContext As Long) As Long
    4. Public Declare Function InternetReadFile Lib "wininet.dll" (ByVal hFile As Long, ByVal sBuffer As String, ByVal lNumBytesToRead As Long, lNumberOfBytesRead As Long) As Integer
    5. Public Declare Function InternetCloseHandle Lib "wininet.dll" (ByVal hInet As Long) As Integer
    6.  
    7. Public Const IF_FROM_CACHE = &H1000000
    8. Public Const IF_MAKE_PERSISTENT = &H2000000
    9. Public Const IF_NO_CACHE_WRITE = &H4000000
    10.        
    11. Private Const BUFFER_LEN = 256
    12.  
    13.  
    14. Public Function GetUrlSource(sURL As String) As String
    15.     Dim sBuffer As String * BUFFER_LEN, iResult As Integer, sData As String
    16.     Dim hInternet As Long, hSession As Long, lReturn As Long
    17.  
    18.     'get the handle of the current internet connection
    19.     hSession = InternetOpen("vb wininet", 1, vbNullString, vbNullString, 0)
    20.     'get the handle of the url
    21.     If hSession Then hInternet = InternetOpenUrl(hSession, sURL, vbNullString, 0, IF_NO_CACHE_WRITE, 0)
    22.     'if we have the handle, then start reading the web page
    23.     If hInternet Then
    24.         'get the first chunk & buffer it.
    25.         iResult = InternetReadFile(hInternet, sBuffer, BUFFER_LEN, lReturn)
    26.         sData = sBuffer
    27.         'if there's more data then keep reading it into the buffer
    28.         Do While lReturn <> 0
    29.             iResult = InternetReadFile(hInternet, sBuffer, BUFFER_LEN, lReturn)
    30.             sData = sData + Mid(sBuffer, 1, lReturn)
    31.         Loop
    32.     End If
    33.    
    34.     'close the URL
    35.     iResult = InternetCloseHandle(hInternet)
    36.  
    37.     GetUrlSource = sData
    38. End Function

    And use it like this:

    VB Code:
    1. Private Sub Command1_Click()
    2.     'Get the HTML source.
    3.     Dim strHTML As String
    4.  
    5.     strHTML = GetURLSource("http://www.google.com")
    6. End Sub

    As for extracting information (a.k.a "parsing"), you'll have to use string manipulation, which usually involves a combination of InStr(), Left$(), Mid$(), Right$(), and maybe InStrRev().

    Depends on what kind of information you're trying to get. I would at least learn how to use those functions first if you don't know how to already.

  3. #3
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Grabbing contents off a webpage.

    Using the web browser control you can interact with a webpage or change its display etc. Here is a good thread with a few of my code examples.

    http://www.vbforums.com/showthread.php?t=330341
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Posts
    65

    Re: Grabbing contents off a webpage.

    Thanks guys, DigiRev thats a big help, is there anyway I can refresh the data after so long?

  5. #5
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: Grabbing contents off a webpage.

    Quote Originally Posted by DarkDemon
    Thanks guys, DigiRev thats a big help, is there anyway I can refresh the data after so long?
    You can put the code in a Timer event instead of a Command Button.

    Something like this (I haven't tested it, but it should work). Add a timer to your form.

    VB Code:
    1. Option Explicit
    2.  
    3. Dim intCounter As Integer 'Number of seconds passed.
    4.  
    5. Dim strHTML As String 'HTML source.
    6.  
    7. '5 second pause time.
    8. 'Change to whatever you want.
    9. Private Const PAUSE_TIME As Integer = 5
    10.  
    11. Private Sub Command1_Click()
    12.     Timer1.Enabled = False 'Stop timer.
    13.     intCounter = 0 'Reset counter (seconds).
    14.    
    15.     Timer1.Enabled = True 'Start timer.
    16. End Sub
    17.  
    18. Private Sub Form_Load()
    19.     Timer1.Enabled = False
    20.     Timer1.Interval = 1000 'Timer_Timer() event will fire every 1000 milliseconds (1 second).
    21. End Sub
    22.  
    23. Private Sub Timer1_Timer()
    24.     Timer1.Enabled = False
    25.    
    26.     intCounter = intCounter + 1 'Increase counter because a second has passed.
    27.    
    28.     'Check if we've paused long enough (5 seconds in this example).
    29.     If intCounter = PAUSE_TIME Then
    30.         strHTML = GetURLSource("http://www.google.com")
    31.         'Process HTML here.
    32.        
    33.         'Reset counter.
    34.         intCounter = 0
    35.     End If
    36.    
    37.     Timer1.Enabled = True
    38. End Sub

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Posts
    65

    Re: Grabbing contents off a webpage.

    Thanks again thats a bunch of help which has pointed me in the right direction.

    My final question is if the webpage contents are as follows

    Name: Bobby
    Age: 23
    Message: Hello everyone


    How split this data up, by grabbing it and showing it in seperate labels. The main reason I'm asking is there is other content around this data.

  7. #7
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: Grabbing contents off a webpage.

    VB Code:
    1. Dim arr() As String
    2.     arr() = Split(WebBrowser1.Document.body.innerText, vbCrLf)
    3.         Label1.Caption = arr(0)
    4.         Label2.Caption = arr(1)
    5.         Label3.Caption = arr(2)

  8. #8
    Lively Member
    Join Date
    Jul 2006
    Posts
    101

    Re: Grabbing contents off a webpage.

    Using the webbrowser control is boring.

  9. #9
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Grabbing contents off a webpage.

    Parsing is easy if the source is short... after awhile it can get out of hand. Using the webbrowser may be boring but it allows you to set up an HTMLDocument... and that object has getElementsByTagName() and other useful methods.

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