Capture text Webrowser

1

Hello, I need to capture a certain text from a website that I am accessing through TWebBrowser , since soon on that internal site it generates a text something like this:

<html>
<head>
<title> XXXXXXXXXXXXXXXXXXXXXXX </title>
<body>
<h2> 1678909 <STRONG style="COLOR: #fff">&shy; Acessos</STRONG></P>

I need to capture only this number before the word ACCESS, how should I proceed? Remembering that even if I take his position using Pos (, it does not matter because with every access on that page the position of the number changes, one hour is in 200 another hour in 300 and so on.

Can anyone save me? Remembering that I need to get only those numbers before access, there will be other numbers on the page, but what I really need to capture are those.

I have no idea how to do this.

    
asked by anonymous 29.12.2015 / 19:30

1 answer

2

Use the native Delphi functions to do this, for example:

vResultado := Trim(Copy(FONTE_TEXTO,1,Pos(' ',FONTE_TEXTO)));

Understand the process:

Trim, vai remover os espaços,
Copy, vai copiar o texto desejado,
FONTE_TEXTO, local onde o texto se encontra,
1, posição de Start da cópia,
Pos, posiciona no final do local ate onde iremos copiar,
' ', é o nosso limitador, ou seja, a posição final da copia,
FONTE_TEXTO, local onde o texto se encontra (nossa fonte do Pos).

Edit:

Following the explanation I gave you just above, you simply change the Start position of the Copy, notice that we copied from position 1 to position X based on the separator which is a space, now the position of Star has changed according to its edition, Let's just change the 1 to a new Search position:

If we know that the text will always be this and formatted in this way, instead of informing 1 we will search with Pos('<h2>',FONTE_TEXTO) the starting position!

For the final position we will search with Pos(' <STRONG ',FONTE_TEXTO)

To work, we need to delete the rest of the found text, so we'll use a temporary variable, I created a procedure that you can use, it would look something like this:

procedure frmTeste.BtnApurarResultadoClick(Sender: TObject);
var
  vTemp : String;
begin
  //Procurando o texto, agora com 2 Delimitadores, 1 inicial e 1 final.
  vTemp := Trim(Copy(FONTE_TEXTO,Pos('<h2> ',FONTE_TEXTO) + 5,Pos(' <STRONG ',FONTE_TEXTO)));
  //Agora deletamos o resto do texto após o número.
  Delete(vTemp,Pos(' ',vTemp),Length(vTemp));
  //Variavel vResultado alimentada somente com o número apurado!
  vResultado := vTemp;
end;
    
29.12.2015 / 19:44