Capture Webrowser text

Asked

Viewed 1,324 times

1

Hello, I need to capture a certain text from a site I am accessing through the TWebBrowser, every time soon on this internal site it generates a text more or less 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 ACCESSES, how should I proceed? Remembering that even if I take his position using Pos(, it doesn’t matter because every access to this page the position of the number changes, an hour is 200 another hour in 300 and so on.

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

I have no idea how to do this.

1 answer

2


Use Delphi’s own native functions for this, follow 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, just change the position of Start Copy, note that we copy from position 1 to position X based on the separator that is a space, now the position of Star changed as your editing, Let’s just change 1 to a new Search position:

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

For the final position we pick up with the Pos(' <STRONG ',FONTE_TEXTO)

To work, we need to delete the rest of the found text, for this we will use a temporary variable, I created a procedure that you can use, it would be 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;
  • thank you for the reply. The problem is, I’m taking the source code from Webbrowser’s page, and putting it in a memo, the source code is huge, you know? I needed a way to put this function before the word "Hits" as I described in the question. Because it appears as follows: 16600 Hits (I need to take only this number before) remembering that this number can reach the millions also and it changes every moment.

  • So friend, with this procedure I passed, no matter the size of the First character set may be '9384238423423428034234230947345345 Hits' it will capture everything before the Hits!

  • the problem that BEFORE this access number, has a giant code you understand ? and the ACCESS number in the middle of the page, in case I will have to find the EXACT position of this number to put in Copy, there is an easier way for me to find this position ?

  • Of course there is, but you need to report it, you reported 1678909 Acessos (esse número muda a cada acesso) so I responded to this case, rsrsrsrs

  • sorry, I corrected the question now. Thank you, if you can help me I thank you.

  • Follow editing in response, we go to tests....

  • Thank you @Junior Moreira

  • a last question @Junior Moreira, in case the text before the number always changes, I will have to do the opposite, for example capture of the word HITS (which is after the number) and cut before it, is it possible also ? cut after the numbers to get to it ? This example of yours gave right, I will mark as response. If you can help me with just one more question I’d appreciate.

  • Da yes, note that here Pos('<h2> ',FONTE_TEXTO) + 5 i locate the <H2> and walk 5 forward positions, I can walk 5 backwards or more if you need!

  • Got it @Júnior Moreira, and this SPACE that has between STRONG needs to have even ? in case I would use - 5 then instead of + ?

  • Exact Positive it positions forwards and Negative backwards! The space I just left because it actually exists between it and the number, but does not need!

  • Okay @Junior Moreira, I will try to do now, any questions I warn you, and thank you so much for your patience.

  • A stupid question @Junior Moreira, why you put + 5 in the case if the number has 7 positions ?

  • It’s not a stupid question, notice that Pos('<h2> ',FONTE_TEXTO) + 5 it positions at the beginning of '<H2> ' counting the characters is = to 5, ie, besides finding the position I already leave the cursor after this text! rsrsrs Hop of the Cat!

  • Got it @Junior Moreira, rsrsrs, thanks again, I’m testing it here!

Show 10 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.