Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Internet (https://www.clubdelphi.com/foros/forumdisplay.php?f=3)
-   -   Como puedo leer una pagina web y almacenarla en una variable (https://www.clubdelphi.com/foros/showthread.php?t=83257)

darkamerico 27-05-2013 18:38:12

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

ecfisa 27-05-2013 20:35:13

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. :)

darkamerico 27-05-2013 22:36:20

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.

darkamerico 27-05-2013 23:15:34

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

MAXIUM 28-05-2013 01:05:15

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

darkamerico 28-05-2013 01:13:32

Hola
 
Debes reclarar la constante a nivel de formulario:

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

Saludos


La franja horaria es GMT +2. Ahora son las 17:50:44.

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