Ever wanted to download a table from any web page--or any section of a web site? The code below downloads a section from a web page and saves it to a .html file. The best way to use this code is to go to a web page you want to grab data from, view the source, get a section of the html code where you want to start grabbing data and plug this into the START variable, do the same with the STOP variable.
The code below grabs the table from the web site http://www.rsp.wisc.edu/html/tab2001-2.html
This code compiles to less than a 20K .exe!!
Code: Select all
;Download .html code from web
;By Lance Jepsen, 7/19/2003
;Based on code by Pille, 14.07.2003
;Adapted from Jost Schwider's VB OpenUrl
;Greetings Pille! Mirabile dictu!
Enumeration
#Window
#Editor
#Url
#cmdOpenUrl
#View
EndEnumeration
defaultUrl.s="http://www.google.fr";Put URL you want to grab data from in here
Procedure.s OpenURL(URL.s, OpenType.b)
isLoop.b=1
INET_RELOAD.l=$80000000
hInet.l=0: hURL.l=0: Bytes.l=0
Buffer.s=Space(2048)
res.s=""
hInet = InternetOpen_("PB@INET", OpenType, #Null, #Null, 0)
hURL = InternetOpenUrl_(hInet, URL, #Null, 0, INET_RELOAD, 0)
Repeat
InternetReadFile_(hURL, @Buffer, Len(Buffer), @Bytes)
If Bytes = 0
isLoop=0
Else
res = res + Left(Buffer, Bytes)
EndIf
Until isLoop=0
Debug res
InternetCloseHandle_(hURL)
InternetCloseHandle_(hInet)
ProcedureReturn res
EndProcedure
If OpenWindow(#Window, 0, 0, 500, 300, "Download", #PB_Window_SystemMenu | #PB_Window_TitleBar | #PB_Window_ScreenCentered)
EditorGadget(#Url, 5, 5, 410, 20)
EditorGadget(#Editor, 5, 30, 490, 260)
;HideGadget(#Url,1)
HideGadget(#Editor,1)
ButtonGadget(#cmdOpenUrl, 420, 5, 75, 20, "Download")
SetGadgetText(#Url, defaultUrl)
Repeat
EventID.l = WaitWindowEvent()
If EventID = #PB_Event_Gadget
Select EventGadget()
Case #cmdOpenUrl
result = MessageRequester("Download","Are you connected to the Internet?",#PB_MessageRequester_YesNo)
If result = #PB_MessageRequester_Yes
ProgressBarGadget(0, 80, 80,250, 25, 0,100)
SetGadgetState (0, 0)
SetGadgetText(#Editor, OpenUrl(GetGadgetText(#Url),1))
SetGadgetState (0, 20)
result$ = OpenUrl(GetGadgetText(#Url),1)
SetGadgetState (0, 80)
If CreateFile(0,"test.html") ; Now we save the data we grabbed to a html file. Open up this file with a web browser to see the data you grabbed.
SetGadgetState (0, 90)
WriteString(0, result$)
SetGadgetState (0, 100)
CloseFile(0)
EndIf
Delay(500)
HideGadget(0,1)
SetGadgetText(#Editor,"Downloading complete. File TEST.HTML created.")
HideGadget(#Editor,0)
DisableGadget(#cmdOpenUrl,1)
EndIf
EndSelect
EndIf
Until EventID = #PB_Event_CloseWindow
EndIf
End