亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 編程 > Delphi > 正文

Delphi通過MSHTML實現一個HTML解析類

2019-11-17 03:54:22
字體:
來源:轉載
供稿:網友
最近經常會模擬網頁提交返回網頁源碼,然后獲得網頁中相應的元素,于是需要常常解析Html中相應的各種元素,網絡是個好東西,搜索一番,就找到了好幾個Delphi版本的HtmlParser的類庫,試著使用了幾個,發現解析起來都不完整,或多或少的回出現一些問題!于是想到了如果界面上有一個瀏覽器,我們可以通過WebBrowser的Document接口對網頁元素進行操作,很是方便!但是模擬網頁提交,界面上是不一定要出現WebBrowser的,肯定有辦法,不通過WebBrowser就直接解析HTML的,那便是我不要WebBrowser這個外殼,只要他里面的Document文檔接口對象就能實現對Html的解析了,查找了一番MSDN,然后Google一下,果然可行,構建方法如下:

//創建IHTMLDocument2接口
  CoCreateInstance(CLASS_HTMLDocument, nil, CLSCTX_INPROC_SERVER, IID_IHTMLDocument2, FHtmlDoc);

接口創建好了之后就能夠對文檔元素進行解析了,很是爽快!

結合了我自己的特有操作,我對Combobox,Table,Frame等一些網頁元素做了相應的封裝,實現了一個HTMLParser,大致代碼如下:

這里只給出聲明,代碼請在最后下載

代碼
(******************************************************)
(*                得閑工作室                          *)
(*              網頁元素操作類庫                      *)
(*                                                    *)
(*              DxHtmlElement Unit                    *)
(*    Copyright(c) 2008-2010  不得閑                  *)
(*    email:appleak46@yahoo.com.cn     QQ:75492895    *)
(******************************************************)
unit DxHtmlElement;

interface
uses Windows,sysUtils,Clipbrd,MSHTML,ActiveX,OleCtrls,Graphics,TypInfo;

{Get EleMent Type}
function IsSelectElement(eleElement: IHTMLElement): Boolean;
function IsPwdElement(eleElement: IHTMLElement): Boolean;
function IsTextElement(element: IHTMLElement): boolean;
function IsTableElement(element: IHTMLElement): Boolean;
function IsElementCollection(element: IHTMLElement): Boolean;
function IsChkElement(element: IHTMLElement): boolean;
function IsRadioBtnElement(element: IHTMLElement): boolean;
function IsMemoElement(element: IHTMLElement): boolean;
function IsFormElement(element: IHTMLElement): boolean;
function IsIMGElement(element: IHTMLElement): boolean;
function IsInIMGElement(element: IHTMLElement): boolean;
function IsLabelElement(element: IHTMLElement): boolean;
function IsLinkElement(element: IHTMLElement): boolean;
function IsListElement(element: IHTMLElement): boolean;
function IsControlElement(element: IHTMLElement): boolean;
function IsObjectElement(element: IHTMLElement): boolean;
function IsFrameElement(element: IHTMLElement): boolean;
function IsInPutBtnElement(element: IHTMLElement): boolean;
function IsInHiddenElement(element: IHTMLElement): boolean;
function IsSubmitElement(element: IHTMLElement): boolean;
{Get ImgElement Data}
function GetPicIndex(doc: IHTMLDocument2; Src: string; Alt: string): Integer;
function GetPicElement(doc: IHTMLDocument2;imgName: string;src: string;Alt: string): IHTMLImgElement;
function GetRegCodePic(doc: IHTMLDocument2;ImgName: string; Src: string; Alt: string): TPicture; overload;
function GetRegCodePic(doc: IHTMLDocument2;Index: integer): TPicture; overload;
function GetRegCodePic(doc: IHTMLDocument2;element: IHTMLIMGElement): TPicture;overload;

type
  TObjectFromLResult = function(LRESULT: lResult;const IID: TIID; WPARAM: wParam;out pObject): HRESULT; stdcall;
  TEleMentType = (ELE_UNKNOW,ELE_TEXT,ELE_PWD,ELE_SELECT,ELE_CHECKBOX,ELE_RADIOBTN,ELE_MEMO,ELE_FORM,ELE_IMAGE,
  ELE_LABEL,ELE_LINK,ELE_LIST,ELE_CONTROL,ELE_OBJECT,ELE_FRAME,ELE_INPUTBTN,ELE_INIMAGE,ELE_INHIDDEN);


function GetElementType(element: IHTMLELEMENT): TEleMentType;
function GetElementTypeName(element: IHTMLELEMENT): string;
function GetHtmlTableCell(aTable: IHTMLTable;aRow,aCol: Integer): IHTMLElement;
function GetHtmlTable(aDoc: IHTMLDocument2; aIndex: Integer): IHTMLTable;
function GetWebBrowserHtmlTableCellText(Doc: IHTMLDocument2;
         const TableIndex, RowIndex, ColIndex: Integer;var ResValue: string):   Boolean;
function GetHtmlTableRowHtml(aTable: IHTMLTable; aRow: Integer): IHTMLElement;

function GetWebBrowserHtmlTableCellHtml(Doc: IHTMLDocument2;
         const TableIndex,RowIndex,ColIndex: Integer;var ResValue: string):   Boolean;
function GeHtmlTableHtml(aTable: IHTMLTable; aRow: Integer): IHTMLElement;
function GetWebBrowserHtmlTableHtml(Doc: IHTMLDocument2;
         const TableIndex,RowIndex: Integer;var ResValue: string):   Boolean;

type
  TDxWebFrameCollection = class;
  TDxWebElementCollection = class;


  TLoadState = (Doc_Loading,Doc_Completed,Doc_Invalidate);

  TDxWebFrame = class
  private
    FFrame: IHTMLWINDOW2;
    FElementCollections: TDxWebElementCollection;
    FWebFrameCollections: TDxWebFrameCollection;
    function GetSrc: string;
    function GetElementCount: integer;
    function GetWebFrameCollections: TDxWebFrameCollection;
    function GetElementCollections: TDxWebElementCollection;
    function GetDocument: IHTMLDOCUMENT2;
    function GetReadState: TLoadState;
    function GetIsLoaded: boolean;
    procedure SetFrame(const Value: IHTMLWINDOW2);
    function GetName: string;
  public
    Constructor Create(IFrame: IHTMLWINDOW2);
    Destructor Destroy;override;
    property Frame: IHTMLWINDOW2 read FFrame write SetFrame;
    property Src: string read GetSrc;
    property Document: IHTMLDOCUMENT2 read GetDocument;
    property Name: string read GetName;
    property Frames: TDxWebFrameCollection read GetWebFrameCollections;
    property ElementCount: integer read GetElementCount;
    property ElementCollections: TDxWebElementCollection read GetElementCollections;
    property ReadyState: TLoadState read GetReadState;
    property IsLoaded: boolean read GetIsLoaded;  
  end;


  TDxWebFrameCollection = Class
  private
    FFrameCollection: IHTMLFramesCollection2;
    Frame: TDxWebFrame;
    function GetCount: integer;
    function GetFrameInterfaceByIndex(index: integer): IHTMLWINDOW2;
    function GetFrameInterfaceByName(Name: string): IHTMLWINDOW2;
    function GetFrameByIndex(index: integer): TDxWebFrame;
    function GetFrameByName(Name: string): TDxWebFrame;
    procedure SetFrameCollection(const Value: IHTMLFramesCollection2);
  public
    Constructor Create(ACollection: IHTMLFramesCollection2);
    Destructor Destroy;override;
    property FrameCollection: IHTMLFramesCollection2 read FFrameCollection write SetFrameCollection;
    property Count: integer read GetCount;
    property FrameInterfaceByIndex[index: integer]: IHTMLWINDOW2 read GetFrameInterfaceByIndex;
    property FrameInterfaceByName[Name: string]: IHTMLWINDOW2 read GetFrameInterfaceByName;

    property FrameByIndex[index: integer]: TDxWebFrame read GetFrameByIndex;
    property FrameByName[Name: string]: TDxWebFrame read GetFrameByName;
  end;
  
  TDxWebElementCollection = class
  private
    FCollection: IHTMLElementCollection;
    FChildCollection:  TDxWebElementCollection;
    function GetCollection(index: String): TDxWebElementCollection;
    function GetCount: integer;
    function GetElement(itemName: string; index: integer): IHTMLElement;
    function GetElementByName(itemName: string): IHTMLELEMENT;
    function GetElementByIndex(index: integer): IHTMLELEMENT;
    procedure SetCollection(const Value: IHTMLElementCollection);
  public
    Constructor Create(ACollection: IHTMLElementCollection);
    Destructor Destroy;override;
    property Collection: IHTMLElementCollection read FCollection write SetCollection;
    property ChildElementCollection[index: String]: TDxWebElementCollection read GetCollection;
    property ElementCount: integer read GetCount;
    property Element[itemName: string;index: integer]: IHTMLElement read GetElement;
    property ElementByName[itemName: string]: IHTMLELEMENT read GetElementByName;
    property ElementByIndex[index: integer]: IHTMLELEMENT read GetElementByIndex;
  end;

  TLinkCollection = class(TDxWebElementCollection)
  
  end;
  TDxWebTable = class;

  TDxTableCollection = class
  private
    FTableCollection: IHTMLElementCollection;
    FDocument: IHTMLDOCUMENT2;
    FWebTable: TDxWebTable;
    function GetTableInterfaceByName(AName: string): IHTMLTABLE;
    procedure SetDocument(Value: IHTMLDOCUMENT2);
    function GetTableInterfaceByIndex(index: integer): IHTMLTABLE;
    function GetCount: integer;
    function GetTableByIndex(index: integer): TDxWebTable;
    function GetTableByName(AName: string): TDxWebTable;
  public
    Constructor Create(Doc: IHTMLDOCUMENT2);
    destructor Destroy;override;
    property TableInterfaceByName[AName: string]: IHTMLTABLE read GetTableInterfaceByName;
    property TableInterfaceByIndex[index: integer]: IHTMLTABLE read GetTableInterfaceByIndex;

    property TableByName[AName: string]: TDxWebTable read GetTableByName;
    property TableByIndex[index: integer]: TDxWebTable read GetTableByIndex;
    
    property Document: IHTMLDOCUMENT2 read FDocument write SetDocument;
    property Count: integer read GetCount;
  end;

  TDxWebTable = class
  private
    FTableInterface: IHTMLTABLE;
    function GetRowCount: integer;
    procedure SetTableInterface(const Value: IHTMLTABLE);
    function GetCell(ACol, ARow: integer): string;
    function GetRowColCount(RowIndex: integer): integer;
    function GetInnerHtml: string;
    function GetInnerText: string;
    function GetCellElement(ACol, ARow: Integer): IHTMLTableCell;
  public
    Constructor Create(ATable: IHTMLTABLE);
    property TableInterface: IHTMLTABLE read FTableInterface write SetTableInterface;
    property RowCount: integer read GetRowCount;
    property Cell[ACol: integer;ARow: integer]: string read GetCell;
    property CellElement[ACol: Integer;ARow: Integer]: IHTMLTableCell read GetCellElement;
    property RowColCount[RowIndex: integer]: integer read GetRowColCount;
    property InnerHtml: string read GetInnerHtml;
    property InnerText: string read GetInnerText;
  end;

  TDxWebCombobox = class
  private
    FHtmlSelect: IHTMLSelectElement;
    function GetCount: Integer;
    procedure SetItemIndex(const Value: Integer);
    function GetItemIndex: Integer;
    function GetName: string;
    procedure SetName(const Value: string);
    function GetValue: string;
    procedure SetValue(const Value: string);
    procedure SetCombInterface(const Value: IHTMLSelectElement);
    function GetItemByName(EleName: string): string;
    function GetItemByIndex(index: integer): string;
    function GetItemAttribute(index: Integer; AttribName: string): OleVariant;
  public
    constructor Create(AWebCombo: IHTMLSelectElement);
    procedure Add(Ele: IHTMLElement);
    procedure Insert(Ele: IHTMLElement;Index: Integer);
    procedure Remove(index: Integer);

    property CombInterface: IHTMLSelectElement read FHtmlSelect write SetCombInterface;
    property Count: Integer read GetCount;
    property ItemIndex: Integer read GetItemIndex write SetItemIndex;
    property ItemByIndex[index: integer]: string read GetItemByIndex;
    property ItemByName[EleName: string]: string read GetItemByName;
    property ItemAttribute[index: Integer;AttribName: string]: OleVariant read GetItemAttribute;
    property Name: string read GetName write SetName;
    property value: string read GetValue write SetValue;
  end;

implementation
end.



HTMLParser解析類的代碼實現單元

代碼
(******************************************************)
(*                得閑工作室                          *)
(*              HTML解析單元庫                        *)
(*                                                    *)
(*              DxHtmlParser Unit                     *)
(*    Copyright(c) 2008-2010  不得閑                  *)
(*    email:appleak46@yahoo.com.cn     QQ:75492895    *)
(******************************************************)
unit DxHtmlParser;

interface
uses Windows,MSHTML,ActiveX,DxHtmlElement,Forms;

type
  TDxHtmlParser = class
  private
    FHtmlDoc: IHTMLDocument2;
    FHTML: string;
    FWebTables: TDxTableCollection;
    FWebElements: TDxWebElementCollection;
    FWebComb: TDxWebCombobox;
    procedure SetHTML(const Value: string);
    function GetWebCombobox(AName: string): TDxWebCombobox;
  public
    constructor Create;
    destructor Destroy;override;
    property HTML: string read FHTML write SetHTML;
    property WebTables: TDxTableCollection read FWebTables;
    property WebElements: TDxWebElementCollection read FWebElements;
    property WebCombobox[Name: string]: TDxWebCombobox read GetWebCombobox;
  end;
implementation

{ TDxHtmlParser }

constructor TDxHtmlParser.Create;
begin
  CoInitialize(nil);
  //創建IHTMLDocument2接口
  CoCreateInstance(CLASS_HTMLDocument, nil, CLSCTX_INPROC_SERVER, IID_IHTMLDocument2, FHtmlDoc);
  Assert(FHtmlDoc<>nil,'構建HTMLDocument接口失敗');
  FHtmlDoc.Set_designMode('On'); //設置為設計模式,不執行腳本
  while not (FHtmlDoc.readyState = 'complete') do
  begin
    sleep(1);
    application.ProcessMessages;
  end;                   
  FWebTables := TDxTableCollection.Create(FHtmlDoc);
  FWebElements := TDxWebElementCollection.Create(nil);
  FWebComb := TDxWebCombobox.Create(nil);
end;

destructor TDxHtmlParser.Destroy;
begin
  FWebTables.Free;
  FWebElements.Free;
  FWebComb.Free;
  CoUninitialize;
  inherited;
end;

function TDxHtmlParser.GetWebCombobox(AName: string): TDxWebCombobox;
begin
   if FWebElements.Collection <> nil then
   begin
     FWebComb.CombInterface := FWebElements.ElementByName[AName] as IHTMLSelectElement;
     Result := FWebComb;
   end
   else Result := nil;
end;

procedure TDxHtmlParser.SetHTML(const Value: string);
begin
  if FHTML <> Value then
  begin
    FHTML := Value;
    FHtmlDoc.body.innerHTML := FHTML;
    FWebElements.Collection := FHtmlDoc.all;
  end;
end;

end.

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
亚洲成人亚洲激情| 中文字幕一区电影| 不卡av在线网站| 亚洲国产天堂网精品网站| 国产亚洲欧美另类中文| 亚洲欧美国产一本综合首页| 国产午夜精品一区二区三区| 宅男66日本亚洲欧美视频| 最近2019年好看中文字幕视频| 国产亚洲激情视频在线| 91av在线免费观看视频| 日韩免费看的电影电视剧大全| 精品久久久久久久中文字幕| 国产午夜精品视频免费不卡69堂| 亚洲激情中文字幕| 欧美成人免费在线视频| 亚洲男人第一av网站| 欧美激情亚洲一区| 456国产精品| 日韩黄色在线免费观看| 久久久999精品视频| 91精品国产高清久久久久久久久| 最近2019免费中文字幕视频三| 成人国内精品久久久久一区| 国产亚洲精品日韩| 日韩国产高清视频在线| 中文字幕精品一区二区精品| 在线看欧美日韩| 91九色精品视频| 国产精品视频精品视频| 日韩中文字幕免费视频| 亚洲视频在线免费观看| 国产精品日本精品| 久久亚洲春色中文字幕| 国产亚洲激情视频在线| 韩国v欧美v日本v亚洲| 欧美成人午夜激情在线| 91嫩草在线视频| 欧美日韩视频免费播放| 韩剧1988在线观看免费完整版| 综合国产在线视频| 久操成人在线视频| 久久免费精品视频| 亚洲成色777777在线观看影院| 日韩亚洲综合在线| 中文字幕国产精品久久| 久久久综合免费视频| 亚洲免费视频观看| 国产91|九色| 国内揄拍国内精品少妇国语| 亚洲欧美在线一区二区| 成人精品一区二区三区电影黑人| 日韩国产欧美精品在线| 欧美性高潮在线| 欧美电影免费观看大全| 国产成人精品久久二区二区91| 国产第一区电影| 国产精品www| 中文字幕日韩在线播放| 91国产高清在线| 少妇高潮久久久久久潘金莲| 国产做受69高潮| 亚洲系列中文字幕| 国产欧美一区二区白浆黑人| 亚洲国产精品va| 国产91av在线| 日韩精品视频观看| 国产精品99久久久久久白浆小说| 亚洲摸下面视频| 91久久中文字幕| 懂色av中文一区二区三区天美| 国产成人精品国内自产拍免费看| 久久夜色撩人精品| 国产精品扒开腿做爽爽爽的视频| 欧美亚洲在线观看| 成人a免费视频| 国产精品无码专区在线观看| 成人免费视频网址| www高清在线视频日韩欧美| 国产精品日韩专区| 国产精品高潮呻吟视频| 久久久999精品| 亚洲国产精品yw在线观看| 国产精品18久久久久久首页狼| 国产精品久久国产精品99gif| 国产精品第七影院| 日韩精品亚洲精品| 亚洲精品永久免费精品| 国产精品亚洲精品| 亚洲第一区在线| 国产丝袜一区二区三区免费视频| 亚洲欧美日韩天堂一区二区| 欧美午夜电影在线| 欧美风情在线观看| 蜜臀久久99精品久久久久久宅男| 麻豆国产精品va在线观看不卡| 国产成人久久久精品一区| 91久久综合亚洲鲁鲁五月天| 青青草精品毛片| 国产精品流白浆视频| 青草青草久热精品视频在线网站| 欧美激情videoshd| 国产精品自拍偷拍视频| 88xx成人精品| 国产在线播放91| 欧美激情亚洲另类| 亚洲欧美自拍一区| 91网在线免费观看| 欧美精品18videos性欧| 日韩精品免费一线在线观看| 久久久久亚洲精品成人网小说| 久久久女女女女999久久| 91精品国产高清久久久久久| 亚洲最大的av网站| 国产精品美女久久久久av超清| 国产精品电影在线观看| 欧美性猛xxx| 日韩精品中文字幕在线| 亚洲国产精品久久久| 青青草一区二区| 亚洲xxxx做受欧美| 国产精品三级美女白浆呻吟| 亚洲天堂网站在线观看视频| 久热99视频在线观看| 一本色道久久综合亚洲精品小说| 最近2019中文免费高清视频观看www99| 亚洲精品黄网在线观看| 国产精品久久久久久久久久新婚| 亚洲国模精品私拍| 亚洲精品网站在线播放gif| 久久久欧美精品| 成人有码在线播放| 国产精品久久77777| 国产一区二区三区四区福利| 亚洲品质视频自拍网| 九九热最新视频//这里只有精品| 国产97在线视频| 国内精品视频久久| 疯狂欧美牲乱大交777| 日韩电影在线观看中文字幕| 人妖精品videosex性欧美| 国产亚洲精品久久久久动| 永久免费毛片在线播放不卡| 国产精品久久久久不卡| 成人免费网站在线| 久久伊人精品视频| 亚洲国产成人av在线| 97在线视频一区| 日韩av观看网址| 国产精品久久久久久久久久久不卡| 日本精品视频在线| 91精品综合久久久久久五月天| 日韩电影中文 亚洲精品乱码| 91高清视频在线免费观看| 亚洲欧美日韩一区二区在线| 国产一区二区香蕉| 亚洲天堂av综合网| 中文字幕日韩专区| 欧洲亚洲免费视频| 狠狠综合久久av一区二区小说| 国产精品白嫩初高中害羞小美女| 91网站在线免费观看| 国产视频一区在线| 欧美精品videos|