最新更新 sitemap 网站制作设计本站搜索
网页设计
国外网站 韩国网站 个人主页 手提袋设计 CSS 网页特效 平面设计 网站设计 Flash CMS技巧 服装网站 php教程 photoshop 画册 服务器选用 数据库 Office
虚拟主机 域名注册 云主机 网页设计 客服QQ:8208442
当前位置:首页 > 编程开发 > asp教程

Delphi通过MSHTML实现HTML解析类技巧

日期:09-05    来源:中国设计秀    作者:cnwebshow.com

最近经常会模拟网页提交返回网页源码,然后获得网页中相应的元素,于是需要常常解析Html中相应的各种元素,网络是个好东西,搜索一番,就找到了好几个Delphi版本的HtmlParser的类库,试着使用了几个,发现解析起来都不完整,或多或少的回出现一些问题!于是想到了如果界面上有一个浏览器,我们可以通过WebBrowser的Document接口对网页元素进行操作,很是方便!但是模拟网页提交,界面上是不一定要出现WebBrowser的,肯定有办法,不通过WebBrowser就直接解析HTML的,那便是我不要WebBrowser这个外壳,只要他里面的Document文档接口对象就能实现对Html的解析了,查找了一番MSDN,然后Google一下,果然可行,构建方法如下:8GY中国设计秀
8GY中国设计秀
//创建IHTMLDocument2接口8GY中国设计秀
  CoCreateInstance(CLASS_HTMLDocument, nil, CLSCTX_INPROC_SERVER, IID_IHTMLDocument2, FHtmlDoc);8GY中国设计秀
8GY中国设计秀
接口创建好了之后就能够对文档元素进行解析了,很是爽快!8GY中国设计秀
8GY中国设计秀
结合了我自己的特有操作,我对Combobox,Table,Frame等一些网页元素做了相应的封装,实现了一个HTMLParser,大致代码如下:8GY中国设计秀
8GY中国设计秀
这里只给出声明,代码请在最后下载 8GY中国设计秀
8GY中国设计秀
代码8GY中国设计秀
(******************************************************)8GY中国设计秀
(*                得闲工作室                          *)8GY中国设计秀
(*              网页元素操作类库                      *)8GY中国设计秀
(*                                                    *)8GY中国设计秀
(*              DxHtmlElement Unit                    *)8GY中国设计秀
(*    Copyright(c) 2008-2010  不得闲                  *)8GY中国设计秀
(*    email:appleak46@yahoo.com.cn     QQ:75492895    *)8GY中国设计秀
(******************************************************)8GY中国设计秀
unit DxHtmlElement;8GY中国设计秀
8GY中国设计秀
interface8GY中国设计秀
uses Windows,sysUtils,Clipbrd,MSHTML,ActiveX,OleCtrls,Graphics,TypInfo;8GY中国设计秀
8GY中国设计秀
{Get EleMent Type}8GY中国设计秀
function IsSelectElement(eleElement: IHTMLElement): Boolean;8GY中国设计秀
function IsPwdElement(eleElement: IHTMLElement): Boolean;8GY中国设计秀
function IsTextElement(element: IHTMLElement): boolean;8GY中国设计秀
function IsTableElement(element: IHTMLElement): Boolean;8GY中国设计秀
function IsElementCollection(element: IHTMLElement): Boolean;8GY中国设计秀
function IsChkElement(element: IHTMLElement): boolean;8GY中国设计秀
function IsRadioBtnElement(element: IHTMLElement): boolean;8GY中国设计秀
function IsMemoElement(element: IHTMLElement): boolean;8GY中国设计秀
function IsFormElement(element: IHTMLElement): boolean;8GY中国设计秀
function IsIMGElement(element: IHTMLElement): boolean;8GY中国设计秀
function IsInIMGElement(element: IHTMLElement): boolean;8GY中国设计秀
function IsLabelElement(element: IHTMLElement): boolean;8GY中国设计秀
function IsLinkElement(element: IHTMLElement): boolean;8GY中国设计秀
function IsListElement(element: IHTMLElement): boolean;8GY中国设计秀
function IsControlElement(element: IHTMLElement): boolean;8GY中国设计秀
function IsObjectElement(element: IHTMLElement): boolean;8GY中国设计秀
function IsFrameElement(element: IHTMLElement): boolean;8GY中国设计秀
function IsInPutBtnElement(element: IHTMLElement): boolean;8GY中国设计秀
function IsInHiddenElement(element: IHTMLElement): boolean;8GY中国设计秀
function IsSubmitElement(element: IHTMLElement): boolean;8GY中国设计秀
{Get ImgElement Data}8GY中国设计秀
function GetPicIndex(doc: IHTMLDocument2; Src: string; Alt: string): Integer;8GY中国设计秀
function GetPicElement(doc: IHTMLDocument2;imgName: string;src: string;Alt: string): IHTMLImgElement;8GY中国设计秀
function GetRegCodePic(doc: IHTMLDocument2;ImgName: string; Src: string; Alt: string): TPicture; overload;8GY中国设计秀
function GetRegCodePic(doc: IHTMLDocument2;Index: integer): TPicture; overload;8GY中国设计秀
function GetRegCodePic(doc: IHTMLDocument2;element: IHTMLIMGElement): TPicture;overload;8GY中国设计秀
8GY中国设计秀
type8GY中国设计秀
  TObjectFromLResult = function(LRESULT: lResult;const IID: TIID; WPARAM: wParam;out pObject): HRESULT; stdcall;8GY中国设计秀
  TEleMentType = (ELE_UNKNOW,ELE_TEXT,ELE_PWD,ELE_SELECT,ELE_CHECKBOX,ELE_RADIOBTN,ELE_MEMO,ELE_FORM,ELE_IMAGE,8GY中国设计秀
  ELE_LABEL,ELE_LINK,ELE_LIST,ELE_CONTROL,ELE_OBJECT,ELE_FRAME,ELE_INPUTBTN,ELE_INIMAGE,ELE_INHIDDEN);8GY中国设计秀
8GY中国设计秀
8GY中国设计秀
function GetElementType(element: IHTMLELEMENT): TEleMentType;8GY中国设计秀
function GetElementTypeName(element: IHTMLELEMENT): string;8GY中国设计秀
function GetHtmlTableCell(aTable: IHTMLTable;aRow,aCol: Integer): IHTMLElement;8GY中国设计秀
function GetHtmlTable(aDoc: IHTMLDocument2; aIndex: Integer): IHTMLTable;8GY中国设计秀
function GetWebBrowserHtmlTableCellText(Doc: IHTMLDocument2;8GY中国设计秀
         const TableIndex, RowIndex, ColIndex: Integer;var ResValue: string):   Boolean;8GY中国设计秀
function GetHtmlTableRowHtml(aTable: IHTMLTable; aRow: Integer): IHTMLElement;8GY中国设计秀
8GY中国设计秀
function GetWebBrowserHtmlTableCellHtml(Doc: IHTMLDocument2;8GY中国设计秀
         const TableIndex,RowIndex,ColIndex: Integer;var ResValue: string):   Boolean;8GY中国设计秀
function GeHtmlTableHtml(aTable: IHTMLTable; aRow: Integer): IHTMLElement;8GY中国设计秀
function GetWebBrowserHtmlTableHtml(Doc: IHTMLDocument2;8GY中国设计秀
         const TableIndex,RowIndex: Integer;var ResValue: string):   Boolean;8GY中国设计秀
8GY中国设计秀
type8GY中国设计秀
  TDxWebFrameCollection = class;8GY中国设计秀
  TDxWebElementCollection = class;8GY中国设计秀
8GY中国设计秀
8GY中国设计秀
  TLoadState = (Doc_Loading,Doc_Completed,Doc_Invalidate);8GY中国设计秀
8GY中国设计秀
  TDxWebFrame = class8GY中国设计秀
  private8GY中国设计秀
    FFrame: IHTMLWINDOW2;8GY中国设计秀
    FElementCollections: TDxWebElementCollection;8GY中国设计秀
    FWebFrameCollections: TDxWebFrameCollection;8GY中国设计秀
    function GetSrc: string;8GY中国设计秀
    function GetElementCount: integer;8GY中国设计秀
    function GetWebFrameCollections: TDxWebFrameCollection;8GY中国设计秀
    function GetElementCollections: TDxWebElementCollection;8GY中国设计秀
    function GetDocument: IHTMLDOCUMENT2;8GY中国设计秀
    function GetReadState: TLoadState;8GY中国设计秀
    function GetIsLoaded: boolean;8GY中国设计秀
    procedure SetFrame(const Value: IHTMLWINDOW2);8GY中国设计秀
    function GetName: string;8GY中国设计秀
  public8GY中国设计秀
    Constructor Create(IFrame: IHTMLWINDOW2);8GY中国设计秀
    Destructor Destroy;override;8GY中国设计秀
    property Frame: IHTMLWINDOW2 read FFrame write SetFrame;8GY中国设计秀
    property Src: string read GetSrc;8GY中国设计秀
    property Document: IHTMLDOCUMENT2 read GetDocument;8GY中国设计秀
    property Name: string read GetName;8GY中国设计秀
    property Frames: TDxWebFrameCollection read GetWebFrameCollections;8GY中国设计秀
    property ElementCount: integer read GetElementCount;8GY中国设计秀
    property ElementCollections: TDxWebElementCollection read GetElementCollections;8GY中国设计秀
    property ReadyState: TLoadState read GetReadState;8GY中国设计秀
    property IsLoaded: boolean read GetIsLoaded;  8GY中国设计秀
  end;8GY中国设计秀
8GY中国设计秀
8GY中国设计秀
  TDxWebFrameCollection = Class8GY中国设计秀
  private8GY中国设计秀
    FFrameCollection: IHTMLFramesCollection2;8GY中国设计秀
    Frame: TDxWebFrame;8GY中国设计秀
    function GetCount: integer;8GY中国设计秀
    function GetFrameInterfaceByIndex(index: integer): IHTMLWINDOW2;8GY中国设计秀
    function GetFrameInterfaceByName(Name: string): IHTMLWINDOW2;8GY中国设计秀
    function GetFrameByIndex(index: integer): TDxWebFrame;8GY中国设计秀
    function GetFrameByName(Name: string): TDxWebFrame;8GY中国设计秀
    procedure SetFrameCollection(const Value: IHTMLFramesCollection2);8GY中国设计秀
  public8GY中国设计秀
    Constructor Create(ACollection: IHTMLFramesCollection2);8GY中国设计秀
    Destructor Destroy;override;8GY中国设计秀
    property FrameCollection: IHTMLFramesCollection2 read FFrameCollection write SetFrameCollection;8GY中国设计秀
    property Count: integer read GetCount;8GY中国设计秀
    property FrameInterfaceByIndex[index: integer]: IHTMLWINDOW2 read GetFrameInterfaceByIndex;8GY中国设计秀
    property FrameInterfaceByName[Name: string]: IHTMLWINDOW2 read GetFrameInterfaceByName;8GY中国设计秀
8GY中国设计秀
    property FrameByIndex[index: integer]: TDxWebFrame read GetFrameByIndex;8GY中国设计秀
    property FrameByName[Name: string]: TDxWebFrame read GetFrameByName;8GY中国设计秀
  end;8GY中国设计秀
  8GY中国设计秀
  TDxWebElementCollection = class8GY中国设计秀
  private8GY中国设计秀
    FCollection: IHTMLElementCollection;8GY中国设计秀
    FChildCollection:  TDxWebElementCollection;8GY中国设计秀
    function GetCollection(index: String): TDxWebElementCollection;8GY中国设计秀
    function GetCount: integer;8GY中国设计秀
    function GetElement(itemName: string; index: integer): IHTMLElement;8GY中国设计秀
    function GetElementByName(itemName: string): IHTMLELEMENT;8GY中国设计秀
    function GetElementByIndex(index: integer): IHTMLELEMENT;8GY中国设计秀
    procedure SetCollection(const Value: IHTMLElementCollection);8GY中国设计秀
  public8GY中国设计秀
    Constructor Create(ACollection: IHTMLElementCollection);8GY中国设计秀
    Destructor Destroy;override;8GY中国设计秀
    property Collection: IHTMLElementCollection read FCollection write SetCollection;8GY中国设计秀
    property ChildElementCollection[index: String]: TDxWebElementCollection read GetCollection;8GY中国设计秀
    property ElementCount: integer read GetCount;8GY中国设计秀
    property Element[itemName: string;index: integer]: IHTMLElement read GetElement;8GY中国设计秀
    property ElementByName[itemName: string]: IHTMLELEMENT read GetElementByName;8GY中国设计秀
    property ElementByIndex[index: integer]: IHTMLELEMENT read GetElementByIndex;8GY中国设计秀
  end;8GY中国设计秀
8GY中国设计秀
  TLinkCollection = class(TDxWebElementCollection)8GY中国设计秀
  8GY中国设计秀
  end;8GY中国设计秀
  TDxWebTable = class;8GY中国设计秀
8GY中国设计秀
  TDxTableCollection = class8GY中国设计秀
  private8GY中国设计秀
    FTableCollection: IHTMLElementCollection;8GY中国设计秀
    FDocument: IHTMLDOCUMENT2;8GY中国设计秀
    FWebTable: TDxWebTable;8GY中国设计秀
    function GetTableInterfaceByName(AName: string): IHTMLTABLE;8GY中国设计秀
    procedure SetDocument(Value: IHTMLDOCUMENT2);8GY中国设计秀
    function GetTableInterfaceByIndex(index: integer): IHTMLTABLE;8GY中国设计秀
    function GetCount: integer;8GY中国设计秀
    function GetTableByIndex(index: integer): TDxWebTable;8GY中国设计秀
    function GetTableByName(AName: string): TDxWebTable;8GY中国设计秀
  public8GY中国设计秀
    Constructor Create(Doc: IHTMLDOCUMENT2);8GY中国设计秀
    destructor Destroy;override;8GY中国设计秀
    property TableInterfaceByName[AName: string]: IHTMLTABLE read GetTableInterfaceByName;8GY中国设计秀
    property TableInterfaceByIndex[index: integer]: IHTMLTABLE read GetTableInterfaceByIndex;8GY中国设计秀
8GY中国设计秀
    property TableByName[AName: string]: TDxWebTable read GetTableByName;8GY中国设计秀
    property TableByIndex[index: integer]: TDxWebTable read GetTableByIndex;8GY中国设计秀
    8GY中国设计秀
    property Document: IHTMLDOCUMENT2 read FDocument write SetDocument;8GY中国设计秀
    property Count: integer read GetCount;8GY中国设计秀
  end;8GY中国设计秀
8GY中国设计秀
  TDxWebTable = class8GY中国设计秀
  private8GY中国设计秀
    FTableInterface: IHTMLTABLE;8GY中国设计秀
    function GetRowCount: integer;8GY中国设计秀
    procedure SetTableInterface(const Value: IHTMLTABLE);8GY中国设计秀
    function GetCell(ACol, ARow: integer): string;8GY中国设计秀
    function GetRowColCount(RowIndex: integer): integer;8GY中国设计秀
    function GetInnerHtml: string;8GY中国设计秀
    function GetInnerText: string;8GY中国设计秀
    function GetCellElement(ACol, ARow: Integer): IHTMLTableCell;8GY中国设计秀
  public8GY中国设计秀
    Constructor Create(ATable: IHTMLTABLE);8GY中国设计秀
    property TableInterface: IHTMLTABLE read FTableInterface write SetTableInterface;8GY中国设计秀
    property RowCount: integer read GetRowCount;8GY中国设计秀
    property Cell[ACol: integer;ARow: integer]: string read GetCell;8GY中国设计秀
    property CellElement[ACol: Integer;ARow: Integer]: IHTMLTableCell read GetCellElement;8GY中国设计秀
    property RowColCount[RowIndex: integer]: integer read GetRowColCount;8GY中国设计秀
    property InnerHtml: string read GetInnerHtml;8GY中国设计秀
    property InnerText: string read GetInnerText;8GY中国设计秀
  end;8GY中国设计秀
8GY中国设计秀
  TDxWebCombobox = class8GY中国设计秀
  private8GY中国设计秀
    FHtmlSelect: IHTMLSelectElement;8GY中国设计秀
    function GetCount: Integer;8GY中国设计秀
    procedure SetItemIndex(const Value: Integer);8GY中国设计秀
    function GetItemIndex: Integer;8GY中国设计秀
    function GetName: string;8GY中国设计秀
    procedure SetName(const Value: string);8GY中国设计秀
    function GetValue: string;8GY中国设计秀
    procedure SetValue(const Value: string);8GY中国设计秀
    procedure SetCombInterface(const Value: IHTMLSelectElement);8GY中国设计秀
    function GetItemByName(EleName: string): string;8GY中国设计秀
    function GetItemByIndex(index: integer): string;8GY中国设计秀
    function GetItemAttribute(index: Integer; AttribName: string): OleVariant;8GY中国设计秀
  public8GY中国设计秀
    constructor Create(AWebCombo: IHTMLSelectElement);8GY中国设计秀
    procedure Add(Ele: IHTMLElement);8GY中国设计秀
    procedure Insert(Ele: IHTMLElement;Index: Integer);8GY中国设计秀
    procedure Remove(index: Integer);8GY中国设计秀
8GY中国设计秀
    property CombInterface: IHTMLSelectElement read FHtmlSelect write SetCombInterface;8GY中国设计秀
    property Count: Integer read GetCount;8GY中国设计秀
    property ItemIndex: Integer read GetItemIndex write SetItemIndex;8GY中国设计秀
    property ItemByIndex[index: integer]: string read GetItemByIndex;8GY中国设计秀
    property ItemByName[EleName: string]: string read GetItemByName;8GY中国设计秀
    property ItemAttribute[index: Integer;AttribName: string]: OleVariant read GetItemAttribute;8GY中国设计秀
    property Name: string read GetName write SetName;8GY中国设计秀
    property value: string read GetValue write SetValue;8GY中国设计秀
  end;8GY中国设计秀
8GY中国设计秀
implementation8GY中国设计秀
end.8GY中国设计秀
8GY中国设计秀
8GY中国设计秀
8GY中国设计秀
HTMLParser解析类的代码实现单元8GY中国设计秀
8GY中国设计秀
代码8GY中国设计秀
(******************************************************)8GY中国设计秀
(*                得闲工作室                          *)8GY中国设计秀
(*              HTML解析单元库                        *)8GY中国设计秀
(*                                                    *)8GY中国设计秀
(*              DxHtmlParser Unit                     *)8GY中国设计秀
(*    Copyright(c) 2008-2010  不得闲                  *)8GY中国设计秀
(*    email:appleak46@yahoo.com.cn     QQ:75492895    *)8GY中国设计秀
(******************************************************)8GY中国设计秀
unit DxHtmlParser;8GY中国设计秀
8GY中国设计秀
interface8GY中国设计秀
uses Windows,MSHTML,ActiveX,DxHtmlElement,Forms;8GY中国设计秀
8GY中国设计秀
type8GY中国设计秀
  TDxHtmlParser = class8GY中国设计秀
  private8GY中国设计秀
    FHtmlDoc: IHTMLDocument2;8GY中国设计秀
    FHTML: string;8GY中国设计秀
    FWebTables: TDxTableCollection;8GY中国设计秀
    FWebElements: TDxWebElementCollection;8GY中国设计秀
    FWebComb: TDxWebCombobox;8GY中国设计秀
    procedure SetHTML(const Value: string);8GY中国设计秀
    function GetWebCombobox(AName: string): TDxWebCombobox;8GY中国设计秀
  public8GY中国设计秀
    constructor Create;8GY中国设计秀
    destructor Destroy;override;8GY中国设计秀
    property HTML: string read FHTML write SetHTML;8GY中国设计秀
    property WebTables: TDxTableCollection read FWebTables;8GY中国设计秀
    property WebElements: TDxWebElementCollection read FWebElements;8GY中国设计秀
    property WebCombobox[Name: string]: TDxWebCombobox read GetWebCombobox;8GY中国设计秀
  end;8GY中国设计秀
implementation8GY中国设计秀
8GY中国设计秀
{ TDxHtmlParser }8GY中国设计秀
8GY中国设计秀
constructor TDxHtmlParser.Create;8GY中国设计秀
begin8GY中国设计秀
  CoInitialize(nil);8GY中国设计秀
  //创建IHTMLDocument2接口8GY中国设计秀
  CoCreateInstance(CLASS_HTMLDocument, nil, CLSCTX_INPROC_SERVER, IID_IHTMLDocument2, FHtmlDoc);8GY中国设计秀
  Assert(FHtmlDoc<>nil,'构建HTMLDocument接口失败');8GY中国设计秀
  FHtmlDoc.Set_designMode('On'); //设置为设计模式,不执行脚本8GY中国设计秀
  while not (FHtmlDoc.readyState = 'complete') do8GY中国设计秀
  begin8GY中国设计秀
    sleep(1);8GY中国设计秀
    application.ProcessMessages;8GY中国设计秀
  end;                   8GY中国设计秀
  FWebTables := TDxTableCollection.Create(FHtmlDoc);8GY中国设计秀
  FWebElements := TDxWebElementCollection.Create(nil);8GY中国设计秀
  FWebComb := TDxWebCombobox.Create(nil);8GY中国设计秀
end;8GY中国设计秀
8GY中国设计秀
destructor TDxHtmlParser.Destroy;8GY中国设计秀
begin8GY中国设计秀
  FWebTables.Free;8GY中国设计秀
  FWebElements.Free;8GY中国设计秀
  FWebComb.Free;8GY中国设计秀
  CoUninitialize;8GY中国设计秀
  inherited;8GY中国设计秀
end;8GY中国设计秀
8GY中国设计秀
function TDxHtmlParser.GetWebCombobox(AName: string): TDxWebCombobox;8GY中国设计秀
begin8GY中国设计秀
   if FWebElements.Collection <> nil then8GY中国设计秀
   begin8GY中国设计秀
     FWebComb.CombInterface := FWebElements.ElementByName[AName] as IHTMLSelectElement;8GY中国设计秀
     Result := FWebComb;8GY中国设计秀
   end8GY中国设计秀
   else Result := nil;8GY中国设计秀
end;8GY中国设计秀
8GY中国设计秀
procedure TDxHtmlParser.SetHTML(const Value: string);8GY中国设计秀
begin8GY中国设计秀
  if FHTML <> Value then8GY中国设计秀
  begin8GY中国设计秀
    FHTML := Value;8GY中国设计秀
    FHtmlDoc.body.innerHTML := FHTML;8GY中国设计秀
    FWebElements.Collection := FHtmlDoc.all;8GY中国设计秀
  end;8GY中国设计秀
end;8GY中国设计秀
8GY中国设计秀
end.8GY中国设计秀

本文引用地址:/bc/article_46177.html
网站地图 | 关于我们 | 联系我们 | 网站建设 | 广告服务 | 版权声明 | 免责声明