Club Delphi  
    Paypal   FTP   CCD     Buscar   Trucos   Trabajo   Foros

Go Back   Foros Club Delphi > Principal > Internet
Register FAQ Members List Calendar Guía de estilo Today's Posts

Colaboración Paypal con ClubDelphi

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 27/05/2013
darkamerico darkamerico is offline
Miembro
 
Join Date: Dec 2010
Posts: 273
Poder: 16
darkamerico Va por buen camino
Smile Como puedo leer una pagina web y almacenarla en una variable

Dentro de mi aplicacion, necesito consultar la pagina: http://www.sbs.gob.pe/0/modulos/jer/...&pfl=0&jer=147

Ya que dentro de ella hay dos elementos llamados:

<p class="WEB_compra">
Compra
<span>2.673</span>
</p>

<p class="WEB_venta">
Venta
<span>2.676</span>
</p>

Que son los Tipos de Cambio de la Superintendencia de Banca y Seguros, pero no deseo utilizar un TWebBrowser, simplemente leer el archivo, luego extraer esos dos elementos y colocarlos en sus respectivos TLabel en la aplicacion.

Estuve investigando y encontre algo como esto:

Código Delphi [-]
procedure TForm1.WebBrowser1DownloadComplete(Sender: TObject);
var
  Document: IHTMLDocument3;
  Collection: IHTMLElementCollection;
  Element: IHTMLElement;
  InputElement: IHTMLInputElement;
begin
  Document := (WebBrowser1.Document as IHTMLDocument3);
  Collection := Document.getElementsByName('WEB_compra');

  if (Collection.length >= 0) then
  begin
    Element := Collection.item(0, 0) as IHTMLElement;
    Element.QueryInterface(IHTMLInputElement, InputElement);

    if Assigned(InputElement) and (InputElement.type_ = 'text') then
      ShowMessage(InputElement.value);
  end;
end;

Sin embargo utilizan un TWebBrowser.

Si alguien me puede dar un alcance, lo agradecería mucho.

Atte.

Americo
Reply With Quote
  #2  
Old 27/05/2013
ecfisa's Avatar
ecfisa ecfisa is offline
Moderador
 
Join Date: Dec 2005
Location: Tres Arroyos, Argentina
Posts: 10,508
Poder: 38
ecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to behold
Hola darkamerico.

Proba de este modo:
Código Delphi [-]
...
uses IdHTTP;

function GetURLText(const aURL: string): string;
begin
  with TIdHTTP.Create(nil) do
  try
    Result := Get(aURL);
  finally
    Free;
  end;
end;

Llamada ejemplo:
Código Delphi [-]
procedure TForm1.Button1Click(Sender: TObject);
const
  URLCNST = 'http://www.sbs.gob.pe/0/modulos/jer/jer_interna.aspx?are=0&pfl=0&jer=147';
var
  Str: string;   
begin
  Str := GetURLText(URLCNST);
  ...
end;

Saludos.
__________________
Daniel Didriksen

Guía de estilo - Uso de las etiquetas - La otra guía de estilo ....
Reply With Quote
  #3  
Old 27/05/2013
darkamerico darkamerico is offline
Miembro
 
Join Date: Dec 2010
Posts: 273
Poder: 16
darkamerico Va por buen camino
Red face Algunos avances

Gracias ecfisa, claro, tu forma soluciona la primera parte de mi problema, ya tengo el contenido de la pagina en una variable, sin embargo, el reto real es acceder al elemento con los ids: Web_Compra y Web_Venta, ya que ellos tienen la informacion que busco.

Saludos.
Reply With Quote
  #4  
Old 27/05/2013
darkamerico darkamerico is offline
Miembro
 
Join Date: Dec 2010
Posts: 273
Poder: 16
darkamerico Va por buen camino
Thumbs up Solucionado

Mis estimados amigos, investigando logre dar con la solucion, chequeen el codigo:

Código Delphi [-]
function ObtenerWeb(webIP : string) : string;
var
  Response: TStringStream;
  HTTP: TIdHTTP;
begin
  Result := '';
  Response := TStringStream.Create('');
  try
    HTTP := TIdHTTP.Create(nil);
    try
      HTTP.Get(webIP, Response);
      if HTTP.ResponseCode = HTTP_RESPONSE_OK then begin
        Result := Response.DataString;
      end else begin
        // TODO -cLogging: add some logging
      end;
    finally
      HTTP.Free;
    end;
  finally
    Response.Free;
  end;
end;

procedure TForm1.Button8Click(Sender: TObject);
var
  doc: OleVariant;
  el: OleVariant;
  i: Integer;
  HTML:string;
begin
  with TIdHTTP.Create(nil) do
  try
    HTML:=ObtenerWeb('http://www.sbs.gob.pe/0/modulos/jer/jer_interna.aspx?are=0&pfl=0&jer=147');
    //HTML:=ObtenerWeb('http://localhost/dolar.html');
    doc := coHTMLDocument.Create as IHTMLDocument2;
    doc.write(HTML);
    doc.close;
    //ShowMessage(doc.body.innerHTML);
    for i := 0 to doc.body.all.length - 1 do
    begin
      el := doc.body.all.item(i);
      if (el.tagName = 'P') and (el.className = 'WEB_compra') then
        ShowMessage(el.innerText);
      if (el.tagName = 'P') and (el.className = 'WEB_venta') then
        ShowMessage(el.innerText);
    end;
  finally
    Free;
  end;
end;

Ahora, surje lo siguiente: Internet es muy lento en mi ciudad, por lo tanto, al pulsar este boton todo el programa se queda congelado, quisiera preguntar de que forma puedo realizar esta consulta en otro Thread de ejecucion, tal cual trabaja Ajax con sus llamadas asincronas...


Un abrazo a todos

Americo
Reply With Quote
  #5  
Old 28/05/2013
MAXIUM's Avatar
MAXIUM MAXIUM is offline
Miembro
 
Join Date: May 2005
Posts: 1,503
Poder: 23
MAXIUM Va camino a la fama
Hola, me arroja error la línea

Código Delphi [-]
if HTTP.ResponseCode = HTTP_RESPONSE_OK then begin
        Result := Response.DataString;
      end else begin

Me dice que HTTP_RESPONSE_OK no esta declarada
Reply With Quote
  #6  
Old 28/05/2013
darkamerico darkamerico is offline
Miembro
 
Join Date: Dec 2010
Posts: 273
Poder: 16
darkamerico Va por buen camino
Hola

Debes reclarar la constante a nivel de formulario:

Código Delphi [-]
const
  HTTP_RESPONSE_OK = 200;

Saludos
Reply With Quote
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
puedo cargar una matriz como variable de sesion? gabrielflowers PHP 5 29/02/2008 21:03
Como puedo leer un archivo de variable... vicvil Varios 4 10/08/2005 22:31
Como leer un archivo TXT dentro de una Página AGAG4 Varios 6 01/09/2004 16:09
Como leer un archivo TXT dentro de una Página AGAG4 Internet 1 31/08/2004 03:19
Como puedo leer las tablas de una BD *.Dat IcebergDelphi Tablas planas 1 10/12/2003 14:47


All times are GMT +2. The time now is 05:30.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi
Copyright 1996-2007 Club Delphi