电脑爱好者,提供IT资讯信息及各类编程知识文章介绍,欢迎大家来本站学习电脑知识。 最近更新 | 联系我们 RSS订阅本站最新文章
电脑爱好者
站内搜索: 
当前位置:首页>> delphi技术>>delphi编程实现获取本地HTML文件的标题,超级链接:

delphi编程实现获取本地HTML文件的标题,超级链接

来源:选择自 levislicn 的 Blog | 2006-3-28 | (有4087人读过)

本文用于提取本地网页的标签元素如<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;


delphi技术热门文章排行
网站赞助商
购买此位置

 

关于我们 | 网站地图 | 文档一览 | 友情链接| 联系我们

Copyright © 2003-2024 电脑爱好者 版权所有 备案号:鲁ICP备09059398号