[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 551
  • Last Modified:

Extract text from web browser

Hi

when i launch a twebbrowser anddirect it to the web page it loads fine.

However, when the document is complete, how can i extract any text begining with the word "pony" into a label.
Meaning.. i dont want to see the extracted text,only say for example if it found 13 "pony" word anywhere on the webpage show it as a form13.label.caption:=(' There have been 13 entries found' )

Please keep is super super simple as i am still learning, nothing compicated please
Thanks
0
boycoder
Asked:
boycoder
  • 16
  • 7
1 Solution
 
Mahdi78Commented:
To load text from Webbrowser to Memo do this

Memo1.Text := WebBrowser1.OleObject.Document.documentElement.innerText;
0
 
boycoderAuthor Commented:
Hi thanks, but thats not what i am looking for for my question.
0
 
boycoderAuthor Commented:
actually you gave me an idea ill try something thankyou mahdi, however please can someone respond to my orginal Question cheers
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
boycoderAuthor Commented:
the url i want to extract the text "PONY" from into a label as a "number" of entries.. is the following.
http://feedback.ebay.co.uk/ws/eBayISAPI.dll?ViewFeedback2&userid=direct

0
 
jimyXCommented:
Here is a code to count any word in a WebBrowser OnComplete event:
uses MSHTML;

function StrCountWord(Word,Str:String):integer;
var
  i:integer;
  txt:string;
begin
  result := 0;
  txt := str;
  repeat
    if pos(Word, txt) > 0 then
      begin
        delete(txt,pos(word,txt),length(word));
        inc(result);
      end;
  until pos(Word, txt) <= 0;
end;

procedure TForm1.WebBrowser1DocumentComplete(ASender: TObject;
  const pDisp: IDispatch; var URL: OleVariant);
var
  Doc : IHTMLDocument2;
  j : Integer;
begin
  If Assigned(WebBrowser1.Document) Then
    begin
      Doc := (WebBrowser1.Document AS IHTMLDocument2);
      form13.label.caption := IntToStr(StrCountWord('pony',Doc.Body.innerText));
    end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Webbrowser1.Navigate('http://feedback.ebay.co.uk/ws/eBayISAPI.dll?ViewFeedback2&userid=direct');
end;

Open in new window

0
 
jimyXCommented:
Small update:
uses MSHTML;

function StrCountWord(Word,Str:String):integer;
var
  i:integer;
  txt:string;
begin
  result := 0;
  txt := str;
  repeat
    if pos(Word, txt) > 0 then
      begin
        delete(txt,pos(word,txt),length(word));
        inc(result);
      end;
  until pos(Word, txt) <= 0;
end;

procedure TForm1.WebBrowser1DocumentComplete(ASender: TObject;
  const pDisp: IDispatch; var URL: OleVariant);
var
  Doc : IHTMLDocument2;
begin
  If Assigned(WebBrowser1.Document) Then
    begin
      Doc := (WebBrowser1.Document AS IHTMLDocument2);
      form13.label.caption := 'There have been '+ IntToStr(StrCountWord('pony',Doc.Body.innerText)) +' entries found';
    end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Webbrowser1.Navigate('http://feedback.ebay.co.uk/ws/eBayISAPI.dll?ViewFeedback2&userid=direct');
end;

Open in new window

0
 
boycoderAuthor Commented:
Thanks that seems to work, but gets it wrong. If i change the username from direct to 0115.9111849 for example which is another random user..

and change the word pony to "xbox"  it only finds 4.  when infact there are many on the page.

when i CTRL and F the page it stops at 4 found which seems to dictate what delphi does. If i continue with the search it finds lots more, but delphi doesnt understand it.

any idea?
0
 
jimyXCommented:
Is it the exact same page that appears on your Internet browser and on the WebBrowser?
Check if you are logged in in the both pages (Internet Browser and the WebBrowser). Could be that the page at the Webbrowser is incomplete as it is for guests or unregistered visitors.

Can you provide the URL and the word you tried so I can give it a shot here.
0
 
boycoderAuthor Commented:
Oh by the way i logged in and all and still the same, however this is one of the few pages on ebay where feedback veiwing is public.
0
 
boycoderAuthor Commented:
i think its not case sensitive. how can i make it so it searches for xbox, XBOX, xbOx ECT..?

also how would i add more words to it, xbox, 360 ect ?
0
 
boycoderAuthor Commented:
so it isnt case sensitive, so it searches for 360, xbox, hello

and finds HELLO, HellO, 360 XbOX ect..

also if it finds mynameisj360 and also mynameisxbox , ignore it.
0
 
jimyXCommented:
It's the case sensitivity. Here is the code updated by adding "UpperCase" to include all search results. Also I have improved the count function:
uses MSHTML;

function StrCountWord(Word,txt:String):integer;
var
  i, j:integer;
begin
  result := 0;
  j:=1;
  repeat
    if posEx(Word, txt,j) > 0 then
      begin
        j := posEx(word,txt,j)+length(word);
        inc(result);
      end;
  until posEx(Word, txt,j) <= 0;
end;

procedure TForm1.WebBrowser1DocumentComplete(ASender: TObject;
  const pDisp: IDispatch; var URL: OleVariant);
var
  Doc : IHTMLDocument2;
begin
  If Assigned(WebBrowser1.Document) Then
    begin
      Doc := (WebBrowser1.Document AS IHTMLDocument2);
      form13.label.caption := 'There have been '+ IntToStr(StrCountWord(UpperCase('pony'),UpperCase(Doc.Body.innerText))) +' entries found';
    end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Webbrowser1.Navigate('http://feedback.ebay.co.uk/ws/eBayISAPI.dll?ViewFeedback2&userid=direct');
end;

Open in new window

0
 
boycoderAuthor Commented:
Thanks, but i always get undeclared posEx, i cannot solve that issue.even with the correct identifer.
0
 
jimyXCommented:
>   "if it finds mynameisj360 and also mynameisxbox , ignore it."

Count function (below) has been updated to ignore the mixture of the targeted word with other words:

>   "how would i add more words to it, xbox, 360 ect ?"

Just call the count function for every word:
Label1.Caption := StrCountWord(UpperCase('xbox'), UpperCase(Doc.Body.innerText));
Label2.Caption := StrCountWord(UpperCase('360'), UpperCase(Doc.Body.innerText));
Label3.Caption := StrCountWord(UpperCase('etc'), UpperCase(Doc.Body.innerText));

Here is the updated code:
uses MSHTML, StrUtils;

function StrCountWord(Word,Txt:String):integer;
var
  i, j, bef, aft:integer;
begin
  result := 0;
  j:=1;
  Txt := UpperCase(' '+Txt+' ');
  word := UpperCase(word);
  repeat
    if posEx(Word, txt, j) > 0 then
      begin
        bef := posEx(Word, txt, j)-1;
        aft := posEx(Word, txt,j)+length(word);
        if not (txt[bef] in ['A'..'Z']) and not (txt[aft] in ['A'..'Z']) then
          inc(result);
        j := posEx(word, txt, j)+length(word);
      end;
  until posEx(word, txt, j) <= 0;
end;

procedure TForm1.WebBrowser1DocumentComplete(ASender: TObject;
  const pDisp: IDispatch; var URL: OleVariant);
var
  Doc : IHTMLDocument2;
begin
  If Assigned(WebBrowser1.Document) Then
    begin
      Doc := (WebBrowser1.Document AS IHTMLDocument2);
      form13.label.caption := 'There have been '+ IntToStr(StrCountWord('pony', Doc.Body.innerText)) +' entries found';
    end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Webbrowser1.Navigate('http://feedback.ebay.co.uk/ws/eBayISAPI.dll?ViewFeedback2&userid=direct');
end;

Open in new window

0
 
boycoderAuthor Commented:
even when i add

uses
strutils;


still same error.
0
 
boycoderAuthor Commented:
iposted sametime as you let me check new code mate
0
 
boycoderAuthor Commented:
i always have an issue with posex =( is there anither way i can never seem to get round that
0
 
jimyXCommented:
>   "i always get undeclared posEx"

We should fix that.

Anyways, here is the count function with "Pos" to avoid using "PosEx":
function StrCountWord(Word,Txt:String):integer;
var
  i, bef, aft:integer;
begin
  result := 0;
  Txt := UpperCase(' '+Txt+' ');
  word := UpperCase(word);
  repeat
    if pos(Word, txt) > 0 then
      begin
        bef := pos(Word, txt)-1;
        aft := pos(Word, txt)+length(word);
        if not (txt[bef] in ['A'..'Z']) and not (txt[aft] in ['A'..'Z']) then
          inc(result);
        delete(txt,1,pos(word,txt)+length(word));
      end;
  until pos(Word, txt) <= 0;
end;

Open in new window

0
 
boycoderAuthor Commented:
it works mate first class. but you said
Count function (below) has been updated to ignore the mixture of the targeted word with other words:

Can you show me where i can edit the words, as they were just examples i gave. Is it possible to actually add the both words to it can avoid in the search ?
0
 
jimyXCommented:
That function is not using the specific words that you gave earlier. I wrote a line that does a test to avoid counting the word when it is among other words:
if not (txt[bef] in ['A'..'Z']) and not (txt[aft] in ['A'..'Z']) then
0
 
boycoderAuthor Commented:
it didnt work when i tested it, it counted 2 xbox words whenone was aomongst an ebayusername. sorry if i conused you and excellent coding it worked fine now for the posex, excellent stuff
0
 
boycoderAuthor Commented:
its ok mate, fantastic stuff... thankyou so so much
0
 
boycoderAuthor Commented:
A+++ This guy knows his stuff
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 16
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now