Solved

How to get the HTML out of TWebBrower?

Posted on 2001-06-25
10
1,212 Views
Last Modified: 2012-08-13
I like to get the HTML out of TWebBrower?

Thanks.

JL
0
Comment
Question by:jlislo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 3
  • +1
10 Comments
 
LVL 6

Expert Comment

by:edey
ID: 6225290
you can try this:

var
 body : variant;
begin
 body := webBrowser1.document;
 memo1.text := body.outerHTML;
end;

GL
Mike
0
 
LVL 6

Expert Comment

by:edey
ID: 6225342
mmmm actually you'd have to try something like this:

var
body : variant;
begin
body := webBrowser1.document;
memo1.text := body.innerHTML;
end;


This will work even if you don't have all the extra MSHTML bits.  A more "proper" way would be to use an IHTMLDocument like so (doesn't work properly in D3):

Q: How can I get the full HTML source?

A: With IE5, you can get the source by using the HTML tags outerHTML property. With IE4 or IE3, you have to save the document to a file and then load the file into a TMemo, TStrings, etc.

var
  HTMLDocument: IHTMLDocument2;
  PersistFile: IPersistFile;
begin
  ...
  HTMLDocument := WebBrowser1.Document as IHTMLDocument2;
  PersistFile := HTMLDocument as IPersistFile;
  PersistFile.Save(StringToOleStr('test.htm'), True);

  while HTMLDocument.readyState <> 'complete' do
    Application.ProcessMessages;
     ...
end;

This tip provided by Ron Loewy (which I found at: http://members.home.com/hfournier).

GL
Mike
0
 

Author Comment

by:jlislo
ID: 6225421
Mike,

I got a error message say "EOleError Method 'outerHTML' not supported by automation object"

I test on a simple HTML page and got this error.

Thanks.

JL
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:jlislo
ID: 6225493
Mike,

Same error I got when I use 'innerHTML'.

I raise the points for a sample code for

1. A TWebBrowser to show the page
2. A memo to display the HTML

Thanks.

JL
0
 
LVL 13

Accepted Solution

by:
Epsylon earned 200 total points
ID: 6225641
Try this:


uses MSHTML;

procedure TForm1.Button1Click(Sender: TObject);
begin
  WebBrowser1.Navigate('av.com');
end;

// TWebBrowser.OnDocumentComplete event
procedure TForm1.WebBrowser1DocumentComplete(Sender: TObject;
  const pDisp: IDispatch; var URL: OleVariant);
var HTMLDocument: IHTMLDocument2;
begin
  Caption := 'ready';
  with WebBrowser1.Document as IHTMLDocument2 do
    Memo1.Text := body.outerHTML;
end;
0
 
LVL 2

Expert Comment

by:RickHalle
ID: 6225766
http://members.home.com/hfournier/

Add the following to the 2nd. post From: edey
Note: You have to import the MSHTML type library and include the resulting MSHTML_TLB, as well as ActiveX, in your Uses clause.

A cleaner approach (I think) is to use an MSXML HTTPRequest.

This link describes adding the MS XML Parser to Delphi:

http://homepages.borland.com/ccalvert/TechPapers/Delphi/XMLSimple/XMLSimple.html

Once the MS XML components are installed then you would drop a XMLHTTPRequest Component on the form and name it objHTTPRequest.

Then add the following to a button click event(or where you need it)

strURL, strHtml: string;
objHTTPRequest.Open('GET', strURL, False);
objHTTPRequest.send;
strHtml := objHTTPRequest.responseText;

This loads the html from the url directly into strHTML. Unlike "body.outerHTML" which just loads the BODY content or "HTMLDocument as IPersistFile" which requires saving the file to disk and then grabbing the contents.

You could also create HTTPRequest at run time but I do not have access to my Delphi yet from this OS to see my code and check it.(Guessing with 40 Gigs of source is just not an option!) If you want to create at run time let me know and I can either boot another OS or I may have this one configured for Delphi by then.

Rick

0
 
LVL 6

Expert Comment

by:edey
ID: 6226417
Hmmm, actually grabbing the data from the parser provides some interesting alt's - esp. if you only want parts of the document.  I'll have to play with that idea a bit :)

GL
Mike
0
 
LVL 2

Expert Comment

by:RickHalle
ID: 6226460
Here's the code to do this by creating the HTTPRequest at run time instead of using the component(The MSXML Parser still needs to be imported into Delphi).

Uses MSXML_TLB;
procedure GetHTTPRequest;
var
  objXmlHTTP: TXMLHTTPRequest;
  strURL, strHtml: string;
begin
  objXmlHTTP := TXMLHTTPRequest.Create(Self);
  objHTTPRequest.Open('GET', strURL, False);
  objHTTPRequest.send;
  strHtml := objHTTPRequest.responseText;
end;

Rick
0
 
LVL 2

Expert Comment

by:RickHalle
ID: 6226471
Whoops, try this. (objHTTPRequest not objXmlHTTP)

Here's the code to do this by creating the HTTPRequest at run time instead of using the component(The
MSXML Parser still needs to be imported into Delphi).

Uses MSXML_TLB;
procedure GetHTTPRequest;
var
 objHTTPRequest: TXMLHTTPRequest;
 strURL, strHtml: string;
begin
 objHTTPRequest := TXMLHTTPRequest.Create(Self);
 objHTTPRequest.Open('GET', strURL, False);
 objHTTPRequest.send;
 strHtml := objHTTPRequest.responseText;
end;

Rick
0
 

Author Comment

by:jlislo
ID: 6227536
Thank you very much.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question