本文用於提取本地網頁的標簽元素如<TITLE></TITLE>,<IMG>,<A></A>...的內容,非常實用於批量文件的操作,這是按一般文件進行文本查找替換無法比擬的,,而這是使用TWEBBROWSER控件無法做到的。類似的,
你可以把本地的Html文件轉換成MHT文件(這是個大家覺得很棘手的問題,本人已經搞定)。
//uses activex,msHtml
function Html_GetTitleFromFile(const HtmlFile:TFileName;var FileTitle:String):Boolean;
var
Idoc : IHtmlDocument2;
//ElementGroup : IHtmlElementCollection;
//HtmlItem: IHtmlElement;
PersistFile: IPersistFile;
begin
Result:=False;
if not fileexists(HtmlFile) then
exit;
FileTitle:='';
try
Idoc := CreateComObject(Class_HTMLDOcument) as IHtmlDocument2;
PersistFile := IDoc as IPersistFile;
if PersistFile.Load(StringToOleStr(HtmlFile),1)<>S_OK then
exit;
IDoc.designMode := 'on'; //This will disable script execution.
{ while IDoc.readyState <> 'complete' do //if it dead here,how to do it?
begin
application.ProcessMessages;
end;
}
// Showmessage(IDoc.readyState);
Application.ProcessMessages;
sleep(1000);
// Showmessage(IDoc.readyState);
if IDoc.readyState<>'complete' then
begin
Application.ProcessMessages;
sleep(1000);
end;
if IDoc.readyState<>'complete' then
begin
IDoc:=nil;
Result:=False;
exit;
end;
Result:=True;
FileTitle:=IDoc.title;
{ //This code also works
ElementGroup:=IDoc.all.tags('TITLE') As IHtmlElementCollection;
HtmlItem:=ElementGroup.item(0,0) As IHtmlElement;
FileTitle:=HtmlItem.innerText;
}
finally
IDoc := nil;
end;
end;