Cómo utilizar Visual FoxPro para descargar una página Web de Internet

Resumen

En este artículo se proporciona código de ejemplo que muestra cómo descargar el contenido de una página Web desde Internet sin interacción del usuario o de automatización de Microsoft Internet Explorer. Un ejemplo utiliza una función de la biblioteca de vínculos dinámicos Urlmon.dll y, otro ejemplo utiliza el control ActiveX Msinet.ocx.

Más información

Urlmon.dll se distribuye con muchos sistemas operativos y Products de Microsoft:
  • Microsoft Internet Explorer 5.0 y versiones posterior
  • Microsoft Windows 98 y Microsoft Windows 98 Segunda edición
  • Microsoft Windows Millennium Edition (Me)
  • Microsoft Windows 2000 Professional y Microsoft Windows 2000 Server
  • Microsoft Office 2000
MSINET.ocx se incluye con Microsoft Visual Studio 97 y Microsoft Visual Studio 6.0. MSINET.ocx también se distribuye con las versiones independientes de los productos siguientes:
  • Microsoft Visual Basic 5.0 y Visual Basic 6.0
  • Microsoft Visual FoxPro 6.0 y en Visual FoxPro 7.0
  • Microsoft Interdev 6.0
  • Microsoft C++ 5.0 y 6.0 de C++
Si eres un usuario con licencia de uno de estos lenguajes de desarrollo, puede redistribuir el control Msinet.ocx.


Cómo utilizar el código de ejemplo

Para usar estos ejemplos de código, siga estos pasos:
  1. Pegue el ejemplo de código en un nuevo programa en Visual FoxPro versión 6.0 o posterior.
  2. Guardar los programas y, a continuación, ejecutar los programas. Como ejecutar cada programa, aparece un formulario que solicita un símbolo de cotización.
  3. Escriba cualquier símbolo de cotización válido en el cuadro de texto y, a continuación, haga clic en Obtener presupuesto para recibir un presupuesto.
Revise los comentarios en el código fuente de cmdGetQuote.click comprender cómo se ha adquirido la oferta.


Nota: La página Web que descargue estos ejemplos de código pueden cambiar en cualquier momento. Como resultado, el código que analiza la página Web para extraer la cotización ya no funcionen. Sin embargo, el concepto que se utiliza para descargar la página todavía se aplicará.

Utiliza Urlmon.dll

*-----------------------------------* AUTHOR: Trevor Hancock (TREVORH@MICROSOFT.COM)
* CREATED: 10/24/01 03:07:28 PM for Microsoft Knowledge Base article Q311306
* ABSTRACT: Downloads a Web page and then parses the Web page to extract a stock quote.
*
* DETAILS: This code uses the function URLDownloadToFile(), located in
* Urlmon.dll. This function downloads a Web page from
* http://moneycentral.msn.com. Specifically, the function calls
* "http://moneycentral.msn.com/scripts/webquote.dll?iPage=lqd&Symbol=",
*passing to the Web page a stock symbol that the user types into THISFORM.txtSYMBOL.
* The Web site returns an entire page of information about the
* stock, which this code then parses to extract the current quote.
*-----------------------------------

PUBLIC ofrmURLMON

ofrmURLMON = NEWOBJECT("frmURLMON")
ofrmURLMON.SHOW
RETURN

**************************
DEFINE CLASS frmURLMON AS FORM
HEIGHT = 83
WIDTH = 317
AUTOCENTER = .T.
BORDERSTYLE = 2
CAPTION = "Stock Quote via URLMON.DLL"
MAXBUTTON = .F.
MINBUTTON = .F.
NAME = "frmURLMON"

ADD OBJECT txtsymbol AS TEXTBOX WITH ;
FONTNAME = "Verdana", ;
VALUE = "MSFT", ;
FORMAT = "!", ;
HEIGHT = 26, ;
LEFT = 94, ;
TABINDEX = 1, ;
TOP = 7, ;
WIDTH = 100, ;
NAME = "txtSymbol"

ADD OBJECT cmdGetQuote AS COMMANDBUTTON WITH ;
TOP = 7, ;
LEFT = 206, ;
HEIGHT = 27, ;
WIDTH = 94, ;
FONTNAME = "Verdana", ;
CAPTION = "Get Quote", ;
TABINDEX = 2, ;
NAME = "cmdGetQuote"

ADD OBJECT lblSymbol AS LABEL WITH ;
AUTOSIZE = .T., ;
FONTNAME = "Verdana", ;
CAPTION = "Stock Symbol:", ;
HEIGHT = 16, ;
LEFT = 3, ;
TOP = 12, ;
WIDTH = 90, ;
TABINDEX = 5, ;
NAME = "lblSymbol"

ADD OBJECT txtQuote AS TEXTBOX WITH ;
FONTNAME = "Verdana", ;
HEIGHT = 23, ;
LEFT = 94, ;
READONLY = .T., ;
TOP = 39, ;
WIDTH = 100, ;
NAME = "txtQuote"

ADD OBJECT lblQuote AS LABEL WITH ;
AUTOSIZE = .T., ;
FONTNAME = "Verdana", ;
CAPTION = "Quote:", ;
HEIGHT = 16, ;
LEFT = 47, ;
TOP = 42, ;
WIDTH = 46, ;
TABINDEX = 5, ;
NAME = "lbQuote"

PROCEDURE cmdGetQuote.CLICK
LOCAL lcQuote AS STRING, ;
lcTempTxtFile AS STRING, ;
lnGetResults AS INTEGER, ;
lcURL AS STRING

lcQuote = ""
*-- Set up a variable referring to a temp .TXT file.
*-- Uses the current VFP TEMP DIR [SYS(2023)]
*-- and a random file name [SYS(2015)]
lcTempTxtFile = FORCEEXT(ADDBS(SYS(2023)) + SYS(2015), "TXT")
lnGetResults = 0
lcGetURl = "http://moneycentral.msn.com/scripts/webquote.dll?iPage=qd&Symbol=" + ;
ALLT(THISFORM.txtsymbol.VALUE)

DECLARE LONG URLDownloadToFile IN URLMON.DLL ;
LONG, STRING, STRING, LONG, LONG

WITH THISFORM
*-- Get the quote using the Urlmon.dll and store it
*-- to a temp .TXT file.
*-- This function returns non-zero if it fails.
lnGetResults = URLDownloadToFile(0, lcGetURl, lcTempTxtFile, 0, 0)
IF lnGetResults # 0
MESSAGEBOX("Download Failed",0,"")
RETURN .F.
ENDIF

*-- Read the Web page into a variable, and then erase it.
lcQuote = FILETOSTR(lcTempTxtFile)
ERASE (lcTempTxtFile)

*-- Grab the quote from the Web page. If in VFP 7.0 or later, we use the new
*-- STREXTRACT() function to grab the data between two delimiters.
*-- In VFP 6.0, use SUBSTR() to get the data, and then trim off any
*-- trailing HTML with the TRANSFORM() AND VAL() functions.
IF VERSION(5) => 700
lcQuote = STREXTRACT(lcQuote, [Last  ], [&nbsp])
ELSE
*-- Extract the quote from the HTML
lcQuote = SUBSTR(lcQuote, ATC([Last  ], lcQuote) + 16, 8)
*-- Trim off the trailing HTML. VAL() will only return the numbers,
*-- which we then change to a string with TRANSFORM()
lcQuote = TRANSFORM(VAL(lcQuote))
ENDIF

*-- Put the quote in the text box.
.txtQuote.VALUE = "$" + lcQuote
ENDWITH
ENDPROC
ENDDEFINE
****************


Usando Msinet.ocx

*-----------------------------------* AUTHOR: Trevor Hancock  (TREVORH@MICROSOFT.COM)
* CREATED: 10/24/01 03:07:28 PM for Microsoft Knowledge Base article Q311306
* ABSTRACT: Downloads a Web page and then parses it to extract a stock quote.
*
* DETAILS: This code uses the Msinet.ocx ActiveX control to
* download a Web page from http://moneycentral.msn.com. Specifically,
* the function calls "http://moneycentral.msn.com/scripts/webquote.dll?iPage=lqd&Symbol=",
* passing to the Web page a stock symbol that the user types into THISFORM.txtSYBMOL.
* The Web site returns an entire page of information about the
* stock, which this code then parses to extract the current quote.
*-----------------------------------

PUBLIC ofrmMSINET

ofrmMSINET = NEWOBJECT("frmMSINET")
ofrmMSINET.SHOW
RETURN

**************************
DEFINE CLASS ocxMSINET AS OLECONTROL
OLECLASS = "InetCtls.Inet.1"
ENDDEFINE

**************************
DEFINE CLASS frmMSINET AS FORM
HEIGHT = 70
WIDTH = 304
AUTOCENTER = .T.
BORDERSTYLE = 3
CAPTION = "Stock Quote via MSINET.OCX"
MAXBUTTON = .F.
MINBUTTON = .F.
NAME = "frmMSINET"

ADD OBJECT MSINET AS ocxMSINET WITH ;
TOP = 37, ;
LEFT = 234, ;
HEIGHT = 100, ;
WIDTH = 100, ;
NAME = "MSINET"

ADD OBJECT txtsymbol AS TEXTBOX WITH ;
FONTNAME = "Verdana", ;
VALUE = "MSFT", ;
FORMAT = "!", ;
HEIGHT = 26, ;
LEFT = 94, ;
TABINDEX = 1, ;
TOP = 7, ;
WIDTH = 100, ;
NAME = "txtSymbol"

ADD OBJECT cmdGetQuote AS COMMANDBUTTON WITH ;
TOP = 7, ;
LEFT = 206, ;
HEIGHT = 27, ;
WIDTH = 94, ;
FONTNAME = "Verdana", ;
CAPTION = "Get Quote", ;
TABINDEX = 2, ;
NAME = "cmdGetQuote"

ADD OBJECT lblSymbol AS LABEL WITH ;
AUTOSIZE = .T., ;
FONTNAME = "Verdana", ;
CAPTION = "Stock Symbol:", ;
HEIGHT = 16, ;
LEFT = 3, ;
TOP = 12, ;
WIDTH = 90, ;
TABINDEX = 5, ;
NAME = "lblSymbol"

ADD OBJECT txtQuote AS TEXTBOX WITH ;
FONTNAME = "Verdana", ;
HEIGHT = 23, ;
LEFT = 94, ;
READONLY = .T., ;
TOP = 39, ;
WIDTH = 100, ;
NAME = "txtQuote"

ADD OBJECT lblQuote AS LABEL WITH ;
AUTOSIZE = .T., ;
FONTNAME = "Verdana", ;
CAPTION = "Quote:", ;
HEIGHT = 16, ;
LEFT = 47, ;
TOP = 42, ;
WIDTH = 46, ;
TABINDEX = 5, ;
NAME = "lbQuote"

PROCEDURE cmdGetQuote.CLICK
LOCAL lcQuote AS STRING, ;
lcURL AS STRING

lcQuote = ""
lcGetURl = "http://moneycentral.msn.com/scripts/webquote.dll?iPage=qd&Symbol=" + ;
ALLT(THISFORM.txtsymbol.VALUE)

WITH THISFORM
*-- Get the quote using the Msinet.ocx ActiveX control.
*-- The control will load it directly into a variable.
lcQuote = .MSINET.OpenURL(lcGetURl)
IF EMPTY(lcQuote)
MESSAGEBOX("Download Failed.",0,"")
RETURN .F.
ENDIF

*-- Grab the quote from the Web page. If in VFP 7.0 or later, we use the new
*-- STREXTRACT() function to grab the data between two delimiters.
*-- In VFP 6.0, use SUBSTR() to get the data, and then trim off any
*-- trailing HTML with the TRANSFORM() AND VAL() functions.
IF VERSION(5) => 700
lcQuote = STREXTRACT(lcQuote, [Last  ], [&nbsp])
ELSE
*-- Extract the quote from the HTML
lcQuote = SUBSTR(lcQuote, ATC([Last  ], lcQuote) + 16, 8)
*-- Trim off the trailing HTML. VAL() will only return the numbers,
*-- which we then change to a string with TRANSFORM()
lcQuote = TRANSFORM(VAL(lcQuote))
ENDIF
*-- Put the quote in the text box.
.txtQuote.VALUE = "$" + lcQuote
ENDWITH
ENDPROC
ENDDEFINE
**************************

Referencias

191222 controles de ActiveX compatibles con Visual FoxPro 6.0

307550 controles de ActiveX compatibles con Visual FoxPro 7.0

Propiedades

Id. de artículo: 311306 - Última revisión: 22 ene. 2017 - Revisión: 1

Comentarios