Solved

How to get the HTML out of TWebBrower?

Posted on 2001-06-25
10
1,216 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
Technology Partners: 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses
Course of the Month11 days, 3 hours left to enroll

631 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