from http://members.shaw.ca/iedelphi/webbrowser.htmQ: 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;
from http://members.shaw.ca/iedelphi/webbrowser.htmQ: 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;
我也来骗分: procedure SaveHTMLSourceToFile(const FileName: string; WB: TWebBrowser); var PersistStream: IPersistStreamInit; FileStream: TFileStream; Stream: IStream; SaveResult: HRESULT; begin PersistStream := WB.Document as IPersistStreamInit; FileStream := TFileStream.Create(FileName, fmCreate); try Stream := TStreamAdapter.Create(FileStream, soReference) as IStream; SaveResult := PersistStream.Save(Stream, True); if FAILED(SaveResult) then MessageBox(Handle, 'Fail to save HTML source', 'Error', 0); finally { we are passing soReference in TStreamAdapter constructor, it is our responsibility to destroy the TFileStream object. } FileStream.Free; end; end;
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;
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;
procedure SaveHTMLSourceToFile(const FileName: string;
WB: TWebBrowser);
var
PersistStream: IPersistStreamInit;
FileStream: TFileStream;
Stream: IStream;
SaveResult: HRESULT;
begin
PersistStream := WB.Document as IPersistStreamInit;
FileStream := TFileStream.Create(FileName, fmCreate);
try
Stream := TStreamAdapter.Create(FileStream, soReference) as IStream;
SaveResult := PersistStream.Save(Stream, True);
if FAILED(SaveResult) then
MessageBox(Handle, 'Fail to save HTML source', 'Error', 0);
finally
{ we are passing soReference in TStreamAdapter constructor,
it is our responsibility to destroy the TFileStream object. }
FileStream.Free;
end;
end;
var TempHtmlFile: String;
begin
Edit1.Text := 'http://www.yahoo.com';
TempHtmlFile := ExtractFilePath (Application.ExeName) + 'temp.html'; //用TIdHTTP控件下載一个Html放入Client
Memo1.Lines.Text := IdHTTP1.Get( Edit1.Text );
//用TMemo控件装Source
Memo1.Lines.SaveToFile( TempHtmlFile );
//用TWebBrowser控件顕示
WebBrowser1.Navigate( TempHtmlFile );
end;
这个仍然不能保存网页中的图片.和flash
procedure ExecWB(cmdID: OLECMDID; cmdexecopt:
OLECMDEXECOPT); overload;
procedure ExecWB(cmdID: OLECMDID; cmdexecopt: OLECMDEXECOPT;
var pvaIn:
OleVariant); overload;
procedure ExecWB(cmdID: rOLECMDID; cmdexecopt:
OLECMDEXECOPT; var pvaIn:
OleVariant; var pvaOut: OleVariant); overload;
另存为:调用“另存为”对话框。
ExecWB(OLECMDID_SAVEAS,OLECMDEXECOPT_DODEFAULT,
EmptyParam,
EmptyParam);