如何获取本地html文件的标题
2016-04-10 19:07
417 查看
本文用于提取本地网页的标签元素如<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;
![](file:///F:/Editor/Editor_good/%E5%A6%82%E4%BD%95%E8%8E%B7%E5%8F%96%E6%9C%AC%E5%9C%B0html%E6%96%87%E4%BB%B6%E7%9A%84%E6%A0%87%E9%A2%98,%E8%B6%85%E7%BA%A7%E9%93%BE%E6%8E%A5_delphi%E6%95%99%E7%A8%8B%EF%BC%8D%E8%99%9A%E6%8B%9F%E4%B8%BB%E6%9C%BA%E8%B5%84%E8%AE%AF%20%E8%99%9A%E6%8B%9F%E4%B8%BB%E6%9C%BA%E5%8A%A8%E6%80%81_files/down_info.asp)
文章整理:西部数码--专业提供域名注册、虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!
你可以把本地的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;
文章整理:西部数码--专业提供域名注册、虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!
相关文章推荐
- html中设置文本字间距
- HTML学习笔记
- Html之表单标签
- html入门(1)
- html入门(2)-标签学习
- html入门(1)
- HTML标签小记
- HTML中target属性中_blank、_self、_parent、_top含义
- 指令:自定义HTML元素和属性
- Example015实现html中checkbox的全选和反选(2)
- Exameple014实现html中checkbox的全选,反选和全不选(1)
- HTML其实就是把页面的数据封装并加上标签
- Html容器机制说明
- HTML中用i作小图标
- HTML写的第一个邮箱登陆界面
- HTML写的第一个邮箱登陆的界面
- HTML常见标签学习与笔记总结
- HDU 1088 Write a simple HTML Browser
- HTML DOM Event 对象
- HTML常见标签学习和笔记总结