Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to get the HTML out of TWebBrower?

Posted on 2001-06-25
10
Medium Priority
?
1,234 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
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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 800 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

916 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