如何通过WebBrowser-Control(ActiveX InternetExplorer)获取页面的源代码? [英] How can I get source code of page thru WebBrowser-Control (ActiveX InternetExplorer)?
问题描述
我有一个xml文档foo.xml。
var
Web:TWebBrowser;
begin
...
Web.Navigate(foo.xml);
//如何通过WebBrower控件获取源代码$ ----
...
end;
WebBrowser1.Navigate()将其加载到RAD组件窗口使用Windows操作系统中的内置IE组件。你所做的是响应回调(对于浏览器组件,双击OnDownloadComplete事件)并将其保存到该函数中的文件中。工作代码片段:
程序TMainForm.WB_SaveAs_HTML(WB:TWebBrowser; const FileName:string);
var
PersistStream:IPersistStreamInit;
流:IStream;
FileStream:TFileStream;
begin
如果没有分配(WB.Document)然后
begin
Logg('Document not loaded!'); //Logg将一行添加到日志文件中。
退出;
结束
PersistStream:= WB.Document as IPersistStreamInit;
FileStream:= TFileStream.Create(FileName,fmCreate);
try
Stream:= TStreamAdapter.Create(FileStream,soReference)as IStream;
如果失败(PersistStream.Save(Stream,True)),那么ShowMessage('SaveAs HTML fail!');
finally
FileStream.Free;
结束
end; (* WB_SaveAs_HTML *)
程序TMainForm.WebBrowser1DownloadComplete(Sender:TObject);
begin
if(WebBrowser1.Document<> nil)AND NOT(WebBrowser1.busy)然后开始
WB_SaveAs_HTML(WebBrowser1,'test.html');
//myStringList.loadFromFile('test.html'); //处理它
结束
结束
请注意,某些MIME(file)类型(如JSON)会提供另存为... '对话框,这将阻止您的阅读,需要手动干预。
How can I get source code of page thru WebBrowser Control (ActiveX InternetExplorer)?
I have an xml document "foo.xml".
var
Web: TWebBrowser;
begin
...
Web.Navigate("foo.xml");
// How can I get source code thru WebBrower control<----
...
end;
WebBrowser1.Navigate() loads it into the RAD component window using the built in IE component in the Windows OS. What you do is respond to a callback (for the browser component, double-click the OnDownloadComplete event) and save it to file in that function. Snippets from working code:
procedure TMainForm.WB_SaveAs_HTML(WB : TWebBrowser; const FileName : string) ;
var
PersistStream: IPersistStreamInit;
Stream: IStream;
FileStream: TFileStream;
begin
if not Assigned(WB.Document) then
begin
Logg('Document not loaded!') ; //'Logg' adds a line to a log file.
Exit;
end;
PersistStream := WB.Document as IPersistStreamInit;
FileStream := TFileStream.Create(FileName, fmCreate) ;
try
Stream := TStreamAdapter.Create(FileStream, soReference) as IStream;
if Failed(PersistStream.Save(Stream, True)) then ShowMessage('SaveAs HTML fail!') ;
finally
FileStream.Free;
end;
end; (* WB_SaveAs_HTML *)
procedure TMainForm.WebBrowser1DownloadComplete(Sender: TObject);
begin
if (WebBrowser1.Document<>nil)AND NOT(WebBrowser1.busy) then begin
WB_SaveAs_HTML(WebBrowser1,'test.html');
//myStringList.loadFromFile('test.html'); //process it.
end;
end;
Note that some MIME ("file") types such as JSON give a 'Save As...' dialog in IE, which stops your reading and requires manual intervention.
这篇关于如何通过WebBrowser-Control(ActiveX InternetExplorer)获取页面的源代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!