本文用于提取本地网页的标签元素如<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;
版权与免责声明
1、本站所发布的文章仅供技术交流参考,本站不主张将其做为决策的依据,浏览者可自愿选择采信与否,本站不对因采信这些信息所产生的任何问题负责。
2、本站部分文章来源于网络,其版权为原权利人所有。由于来源之故,有的文章未能获得作者姓名,署“未知”或“佚名”。对于这些文章,有知悉作者姓名的请告知本站,以便及时署名。如果作者要求删除,我们将予以删除。除此之外本站不再承担其它责任。
3、本站部分文章来源于本站原创,本站拥有所有权利。
4、如对本站发布的信息有异议,请联系我们,经本站确认后,将在三个工作日内做出修改或删除处理。
请参阅权责声明!