How can I get HTML source code from TWebBrowser

后端 未结 4 1419
逝去的感伤
逝去的感伤 2020-12-17 18:29

How can I get source code from WebBrowser component?

I want to get source code of active page on WebBrowser component and write it to a Memo component.

Thank

相关标签:
4条回答
  • 2020-12-17 19:17

    You can use the IPersistStreamInit Interface and the save method to store the content of the Webbrowser in a Stream.

    Uses 
      ActiveX;
    
    function GetWebBrowserHTML(const WebBrowser: TWebBrowser): String;
    var
      LStream: TStringStream;
      Stream : IStream;
      LPersistStreamInit : IPersistStreamInit;
    begin
      if not Assigned(WebBrowser.Document) then exit;
      LStream := TStringStream.Create('');
      try
        LPersistStreamInit := WebBrowser.Document as IPersistStreamInit;
        Stream := TStreamAdapter.Create(LStream,soReference);
        LPersistStreamInit.Save(Stream,true);
        result := LStream.DataString;
      finally
        LStream.Free();
      end;
    end;
    
    0 讨论(0)
  • 2020-12-17 19:19

    That works well too:

        uses MSHTML;
    
        function GetHTML(w: TWebBrowser): String;
        Var
          e: IHTMLElement;
        begin
          Result := '';
          if Assigned(w.Document) then
          begin
             e := (w.Document as IHTMLDocument2).body;
        
             while e.parentElement <> nil do
             begin
               e := e.parentElement;
             end;
        
             Result := e.outerHTML;
          end;
        end;
    
    0 讨论(0)
  • 2020-12-17 19:20

    This has been asked and answered many times in the Embarcadero forums, with plenty of code examples posted. Search the archives.

    The gist of it is that you Navigate() to the desired URL and wait for the OnDocumentComplete event to fire, then QueryInterface() the Document property for the IPersistStreamInit interface and call its save() method. Create a TStream object instance, such as a TMemoryStream, wrap it in a TStreamAdapter object, and then pass the adapter to save(). You can then load the TStream into the TMemo as needed.

    0 讨论(0)
  • 2020-12-17 19:27

    Why not Quick and Dirty:

    OnNavigateComplete2()
    
    Form1.RichEdit1.Text:=(WebBrowser1.OleObject.Document.documentElement.outerhtml);
    
    0 讨论(0)
提交回复
热议问题