• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

htmlParserfordelphi

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
(******************************************************)
(* **工作室 *)
(* HTML解析单元库 *)
(* *)
(* DxHtmlParser Unit *)
(* *)
(* email:[email protected] QQ:316454904 *)
(******************************************************)
unit MyHtmlParser;

interface
uses Windows,MSHTML,ActiveX,Forms,Variants, SysUtils, Classes;

type
TMyHtmlParser = class
private
Doc: IHTMLDocument2;
FHTML, FURL: string;
procedure SetHTML(const Value: string);
procedure SetURL(s: string);
public
Doc2:IHTMLDocument2;
FParserOK:boolean;
FTimeOut:integer;
constructor Create;
destructor Destroy;override;
property HTML: string read FHTML write SetHTML;
property URL: string read FURL write SetURL;
property TimeOut:integer read FTimeOut write FTimeOut default 20000;
property ParserOK:boolean read FParserOK default false;
end;
implementation

{ TDxHtmlParser }

procedure TMyHtmlParser.SetURL(s: string);
var
doc4:ihtmldocument4;
tick:integer;
begin
FURL:=s ;
if FURL<>'' then
begin
tick:=gettickcount;
doc.QueryInterface(IID_ihtmldocument4,doc4);
if assigned(doc4) then
begin
doc2:=doc4.createDocumentFromUrl(s,'null');
while (doc2.readyState<>'complete') and (gettickcount-tick<FTimeOut) do
begin
application.ProcessMessages;
sleep(10);
end;
if doc2.readyState='complete' then FParserOK:=true;
end;
end;
end;

constructor TMyHtmlParser.Create;
begin
CoInitialize(nil);
//创建IHTMLDocument2接口
FTimeOut:=20000;
CoCreateInstance(CLASS_HTMLDocument, nil, CLSCTX_INPROC_SERVER, IID_IHTMLDocument2, Doc);
Assert(Doc<>nil,'构建HTMLDocument接口失败');
Doc.Set_designMode('On'); //设置为设计模式,不执行脚本
while not (Doc.readyState = 'complete') do
begin
sleep(1);
Application.ProcessMessages;
end;
HTML:='<html></html>';
end;

destructor TMyHtmlParser.Destroy;
begin
CoUninitialize;
inherited;
end;

procedure TMyHtmlParser.SetHTML(const Value: string);
var
V: OLEVariant;
vDocument: OLEVariant;
vMimeType: OLEVariant;
vHtml: OLEVariant;
tick:integer;
begin
if FHTML <> Value then
begin
tick:=gettickcount;
FHTML := Value;
V := Doc;
vDocument := V.script.Document;
vMimeType := 'text/Html';
vHtml := FHtml;
vDocument.Open(vMimeType);
vDocument.Clear;
vDocument.Write(vHtml);
vDocument.Close;
while (doc.readyState<>'complete') and (gettickcount-tick<FTimeOut) do
begin
application.ProcessMessages;
sleep(10);
end;
if doc.readyState='complete' then
begin
FParserOK:=true;
doc2:=doc;
end;
end;
end;

end.

受到得闲老师的htmlparser启发,完善了一下,去掉的自认为没必要的东西(有了IhtmlDocument2,神马都是浮云),当然不是完全抄自得闲老师的解析器,本单元中的精华是SetHTML(const Value: string);和SetURL(s: string);这两个函数,其它的没什么意思。

SetHTML(const Value: string)是抄自TEmbeddedwb的IEParser。

SetURL(s: string);是根据MSDN上ihtmlDocument4.createDocumentFromUrl创建出新的ihtmlDocument2接口。

不解释了,代码就这点。

不足的地方:doc2会自动去下载图片,如有朋友修改后还请发我一份,谢谢!!


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
DELPHI2010安装Comport4发布时间:2022-07-18
下一篇:
Delphi高效定制格式的FormatDateTime发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap