Solved

How to get the HTML out of TWebBrower?

Posted on 2001-06-25
10
1,196 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to fill array with TArray.Create? 14 95
Delphi IDE crash without error message ... 7 99
URL for downloading Google Chrome for Win XP 2 183
enhance the following code 3 37
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…
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…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

861 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