|
-
Dec 2nd, 2006, 04:38 AM
#1
Thread Starter
Lively Member
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
-
Dec 2nd, 2006, 05:08 AM
#2
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:
Option Explicit
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
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
Public Declare Function InternetReadFile Lib "wininet.dll" (ByVal hFile As Long, ByVal sBuffer As String, ByVal lNumBytesToRead As Long, lNumberOfBytesRead As Long) As Integer
Public Declare Function InternetCloseHandle Lib "wininet.dll" (ByVal hInet As Long) As Integer
Public Const IF_FROM_CACHE = &H1000000
Public Const IF_MAKE_PERSISTENT = &H2000000
Public Const IF_NO_CACHE_WRITE = &H4000000
Private Const BUFFER_LEN = 256
Public Function GetUrlSource(sURL As String) As String
Dim sBuffer As String * BUFFER_LEN, iResult As Integer, sData As String
Dim hInternet As Long, hSession As Long, lReturn As Long
'get the handle of the current internet connection
hSession = InternetOpen("vb wininet", 1, vbNullString, vbNullString, 0)
'get the handle of the url
If hSession Then hInternet = InternetOpenUrl(hSession, sURL, vbNullString, 0, IF_NO_CACHE_WRITE, 0)
'if we have the handle, then start reading the web page
If hInternet Then
'get the first chunk & buffer it.
iResult = InternetReadFile(hInternet, sBuffer, BUFFER_LEN, lReturn)
sData = sBuffer
'if there's more data then keep reading it into the buffer
Do While lReturn <> 0
iResult = InternetReadFile(hInternet, sBuffer, BUFFER_LEN, lReturn)
sData = sData + Mid(sBuffer, 1, lReturn)
Loop
End If
'close the URL
iResult = InternetCloseHandle(hInternet)
GetUrlSource = sData
End Function
And use it like this:
VB Code:
Private Sub Command1_Click()
'Get the HTML source.
Dim strHTML As String
strHTML = GetURLSource("http://www.google.com")
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.
-
Dec 2nd, 2006, 05:09 AM
#3
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Dec 2nd, 2006, 06:09 AM
#4
Thread Starter
Lively Member
Re: Grabbing contents off a webpage.
Thanks guys, DigiRev thats a big help, is there anyway I can refresh the data after so long?
-
Dec 2nd, 2006, 06:19 AM
#5
Re: Grabbing contents off a webpage.
 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:
Option Explicit
Dim intCounter As Integer 'Number of seconds passed.
Dim strHTML As String 'HTML source.
'5 second pause time.
'Change to whatever you want.
Private Const PAUSE_TIME As Integer = 5
Private Sub Command1_Click()
Timer1.Enabled = False 'Stop timer.
intCounter = 0 'Reset counter (seconds).
Timer1.Enabled = True 'Start timer.
End Sub
Private Sub Form_Load()
Timer1.Enabled = False
Timer1.Interval = 1000 'Timer_Timer() event will fire every 1000 milliseconds (1 second).
End Sub
Private Sub Timer1_Timer()
Timer1.Enabled = False
intCounter = intCounter + 1 'Increase counter because a second has passed.
'Check if we've paused long enough (5 seconds in this example).
If intCounter = PAUSE_TIME Then
strHTML = GetURLSource("http://www.google.com")
'Process HTML here.
'Reset counter.
intCounter = 0
End If
Timer1.Enabled = True
End Sub
-
Dec 2nd, 2006, 08:52 AM
#6
Thread Starter
Lively Member
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.
-
Dec 3rd, 2006, 01:50 PM
#7
Re: Grabbing contents off a webpage.
VB Code:
Dim arr() As String
arr() = Split(WebBrowser1.Document.body.innerText, vbCrLf)
Label1.Caption = arr(0)
Label2.Caption = arr(1)
Label3.Caption = arr(2)
-
Dec 4th, 2006, 02:28 AM
#8
Lively Member
Re: Grabbing contents off a webpage.
Using the webbrowser control is boring.
-
Dec 4th, 2006, 06:25 AM
#9
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|