Programming Delphi How to get a web page's html source by using TWebBrowser component in delphi
Notice: Undefined variable: relatedArticlesCount in /home/wwwroot/codeback.net/plugins/content/tags.php on line 149

To get a webpage's html source code is very easy via the TWebBrowser component. Let us look an example.

Create a new project, drag a TWebBrowser, a TMemo and a TButton component onto the form. Adding codes as below shows.

uses mshtml;

//Loading a page when the program runs
procedure TForm1.FormCreate(Sender: TObject) ;
begin
  WebBrowser1.Navigate('http://codeback.net') ;
end;

//In the OnClick events of button1, adding codes.
procedure TForm1.Button1Click(Sender: TObject);
var
   iElement : IHTMLElement;
begin
   if Assigned(WebBrowser1.Document) then
   begin
     iElement := (WebBrowser1.Document AS IHTMLDocument2).body;

     while iElement.parentElement nil do
     begin
       iElement := iElement.parentElement;
     end;
     memo1.Text := iElement.outerHTML;
   end;
end;

After the page loading complete, click the button, then you would find the memo1 shows the html source. If you are using the C++Builder, see the code example here: 

How to Get the Content of Html Source From TCppWebBrowser in C Builder .


 


Add comment


Security code
Refresh

Programming - Delphi