本文用于提取本地網頁的標簽元素如<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;
新聞熱點
疑難解答