Solved

How to get the HTML out of TWebBrower?

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

Thanks.

JL
0
Comment
Question by:jlislo
  • 3
  • 3
  • 3
  • +1
10 Comments
 
LVL 6

Expert Comment

by:edey
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:jlislo
Comment Utility
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
Comment Utility
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 2

Expert Comment

by:RickHalle
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thank you very much.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

771 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now