Recientemente, a menudo simulan los envíos de la página web y regreso al código fuente de la página web, y luego obtengo los elementos correspondientes en la página web, necesito analizar los varios elementos correspondientes en HTML. Buscando, encontré varias versiones de Delphi de las clases de HTMLParser. Así que pensé que si hay un navegador en la interfaz, podemos operar elementos de la página web a través de la interfaz de documentos de WebBrowser, ¡lo cual es muy conveniente! Sin embargo, si simula el envío de la página web, WebBrowser no necesariamente aparece en la interfaz. El objeto en su interior puede realizar html después de analizarlo, buscar MSDN, y luego Google, funciona, y el método de construcción es el siguiente:
// Crear interfaz IhtmlDocument2
CoCreateInstance (class_htmlDocument, nil, clsctx_inproc_server, iid_ihtmlDocument2, fhtmldoc);
Después de crear la interfaz, los elementos del documento se pueden analizar, ¡lo cual es muy refrescante!
Combinando mis propias operaciones únicas, he encapsulado algunos elementos de la página web como Combobox, Tabla, Frame, etc. e implementado un HTMLParser.
Solo se da una declaración aquí, descargue el código al final
Código
(****************************************************** *****)
(*Estudio dexiano*)
(*Biblioteca de clases de operación del elemento de la página web*)
(**)
(*Unidad DXHTMLELEMENT*)
(*Copyright (c) 2008-2010 sin inactivo*)
(*Correo electrónico: [email protected]: 75492895*)
(****************************************************** *****)
Unidad DXHTMLEMENT;
interfaz
Utiliza Windows, Sysutils, Cipbrd, MSHTML, ActiveX, Olectrls, Graphics, Typinfo;
{Get Tipo de elemento}
función isselectelement (elelement: ihtmlelement): boolean;
función ispwdelement (elelement: ihtmlelement): boolean;
función istextelement (elemento: ihtmlelement): boolean;
función istableElement (elemento: ihtmlelement): boolean;
función iselementCollection (elemento: ihtmlelement): boolean;
función ischkelement (elemento: ihtmlelement): boolean;
función isradiobtnelement (elemento: ihtmlelement): boolean;
función ismemoElement (elemento: ihtmlelement): boolean;
función isformeLement (elemento: ihtmlelement): boolean;
función isimgelement (elemento: ihtmlelement): boolean;
función isinimgelement (elemento: ihtmlelement): boolean;
función islabelelement (elemento: ihtmlelement): boolean;
función islinkelement (elemento: ihtmlelement): boolean;
función islistelement (elemento: ihtmlelement): boolean;
función iScontroleLement (elemento: ihtmlelement): boolean;
función isobjectelement (elemento: ihtmlelement): boolean;
función isFrameElement (elemento: ihtmlelement): boolean;
función esinputBtnelement (elemento: ihtmlelement): boolean;
función isinhiddenElement (elemento: ihtmlelement): boolean;
función isSubMitElement (Elemento: ihtmlelement): boolean;
{Obtener datos de imgelement}
function getPicIndex (doc: ihtmlDocument2; src: string; alt: string): integer;
función getPiCelement (doc: ihtmlDocument2; imgname: string; src: string; alt: string): ihtmlimgelement;
function getRegCodePic (doc: ihtmlDocument2; imgname: string; src: string; alt: string): tpicture;
function getRegCodepic (DOC: IHTMLDOCUMENT2; índice: entero): tpicture;
función getRegCodePic (doc: ihtmlDocument2; elemento: ihtmlimgelement): tpicture; sobrecarga;
tipo
TobjectFromlResult = function (lResult: lResult; const iid: tiid; wparam: wparam; out pods): hResult;
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);
función getElementType (elemento: ihtmlelement): TelementType;
función getElementTyPeName (elemento: ihtmlelement): string;
función gethtmltableCell (Atable: ihtmltable; arow, acol: entero): ihtmlelement;
función gethtmltable (ADOC: ihtmlDocument2; aindex: integer): ihtmltable;
function getWebBrowserHtmltableCellText (doc: ihtmlDocument2;
const tableindex, rowIndex, colindex: integer; var revalue: string): boolean;
función gethtmltableRowhtml (Atable: ihtmltable; arow: integer): ihtmlelement;
función getWebbrowserHtmltableCellhtml (doc: ihtmlDocument2;
const tableindex, rowIndex, colindex: integer; var revalue: string): boolean;
función gehtmltablehtml (Atable: ihtmltable; arow: entero): ihtmlelement;
función getWebbrowserHtmltableHtml (DOC: ihtmlDocument2;
const tableIndex, rowIndex: entero; var revalue: string): boolean;
tipo
TdxwebframeCollection = class;
TdxwebelementCollection = class;
Tloadstate = (doc_loading, doc_completed, doc_invalidate);
Tdxwebframe = clase
Privado
FFRame: ihtmlwindow2;
FelementCollections: TDXWebelementCollection;
FwebframeCollections: tdxwebframeCollection;
función getSrc: string;
función getElementCount: Integer;
function getWebframeCollections: tdxwebframeCollection;
función getElementCollections: tdxwebelementCollection;
función getDocument: ihtmlDocument2;
función getReadstate: tloadState;
función getSloaded: boolean;
procedimiento setframe (valor const: ihtmlwindow2);
función getName: string;
público
Constructor create (iframe: ihtmlwindow2);
Destructor destruir; anular;
Marco de propiedades: ihtmlwindow2 lea fframe escritura setframe;
Propiedad Src: String Read getSrc;
Documento de propiedad: ihtmlDocument2 lea getDocument;
Nombre de la propiedad: cadena leer getName;
Marcos de propiedad: TDXWEBFRAMECOLCELection Read GetWebFrameCollections;
Propiedad ElementCount: Integer Read GetElementCount;
Propiedad ElementCollections: TDXWebElementCollection Read getElementCollections;
Propiedad ReadyState: tloadState Read getReadState;
Propiedad Isloged: Boolean Read getSloaded;
fin;
TdxwebframeCollection = clase
Privado
FframeCollection: ihtmlframesCollection2;
Marco: tdxwebframe;
función getCount: integer;
función getFrameInterfaceByIndex (índice: entero): ihtmlwindow2;
función getFrameInterfaceByName (nombre: cadena): ihtmlwindow2;
función getFrameByIndex (índice: entero): tdxwebframe;
función getFrameByName (nombre: cadena): tdxwebframe;
procedimiento setframeCollection (valor const: ihtmlframesCollection2);
público
Constructor create (acollection: ihtmlframesCollection2);
Destructor destruir; anular;
Propiedad FrameCollection: ihtmlFramesCollection2 Read FFRAMECOLLECTION Escribir setFrameCollection;
Recuento de propiedades: Integer Read GetCount;
Property FrameInterfaceByIndex [índice: integer]: ihtmlwindow2 read getFrameInterfaceByIndex;
Property FrameInterfaceByName [nombre: cadena]: ihtmlwindow2 read getFrameInterfaceByName;
Propiedad FrameByIndex [índice: integer]: tdxwebframe read getFrameByIndex;
Propiedad FrameByName [Nombre: String]: TDXWebFrame Read getFrameByName;
fin;
TdxwebelementCollection = class
Privado
Fcollection: ihtmlelementCollection;
FchildCollection: tdxwebelementCollection;
función getCollection (índice: string): tdxwebelementCollection;
función getCount: integer;
función getElement (itemname: string; index: entero): ihtmlelement;
function getElementByName (itemname: string): ihtmlelement;
función getElementByIndex (índice: entero): ihtmlelement;
procedimiento setCollection (valor const: ihtmlelementCollection);
público
Constructor create (acollection: ihtmlelementCollection);
Destructor destruir; anular;
Colección de propiedades: ihtmlelementCollection lea fcollection write setCollection;
Propiedad ChildElementCollection [índice: cadena]: tdxwebelementCollection Read getCollection;
Propiedad ElementCount: Integer Read GetCount;
Elemento de propiedad [itemname: string; index: entero]: ihtmlelement le read getElement;
propiedad ElementByName [itemName: String]: ihtmlelement lea getElementByName;
propiedad ElementByIndex [índice: integer]: ihtmlelement lea getElementByInx;
fin;
TlinkCollection = class (tdxwebelementCollection)
fin;
Tdxwebtable = class;
TdxtableCollection = clase
Privado
FtableCollection: ihtmlelementCollection;
FDocument: ihtmlDocument2;
FwebTable: tdxwebTable;
function getTableInterfaceByName (aname: string): ihtmltable;
procedimiento setDocument (valor: ihtmlDocument2);
función getTableInterfaceByIndex (índice: entero): ihtmltable;
función getCount: integer;
función getTableByIndex (índice: entero): tdxwebtable;
función getTableByName (aname: string): tdxwebtable;
público
Constructor create (doc: ihtmlDocument2);
destructor destruir; anular;
Propiedad TablainterfaceByName [aname: string]: ihtmltable read getTableInterfaceByName;
Propiedad TablainterfaceByIndex [índice: integer]: ihtmltable read getTableInterfaceByInx;
Propiedad TableByName [aname: string]: tdxwebtable read getTableByName;
Propiedad TablaByIndex [índice: Integer]: TDXWebTable Read getTableByInx;
Documento de propiedad: ihtmlDocument2 lea fDocument Write setDocument;
Recuento de propiedades: Integer Read GetCount;
fin;
Tdxwebtable = class
Privado
FtableInterface: ihtmltable;
función getRowCount: Integer;
procedimiento settableInterface (valor const: ihtmltable);
función getCell (acol, arow: entero): string;
función getRowColCount (RowIndex: Integer): Integer;
función getinnerhtml: string;
function getInntext: String;
función getCellelement (acol, arow: entero): ihtmltableCell;
público
Constructor create (atable: ihtmltable);
Propiedad Tablainterface: IHTMLTable Read ftableInterface Escribir settableInterface;
Propiedad RowCount: Integer Read GetrowCount;
Propiedad Cell [Acol: Integer; Arow: Integer]: String Read GetCell;
Propiedad CellElement [Acol: Integer; Arow: Integer]: ihtmltableCell Read getCellelement;
Propiedad RowColCount [RowIndex: Integer]: Integer Read getRowColCount;
propiedad innerhtml: cadena leer getinnerhtml;
Propiedad Inntext: String Read getInntext;
fin;
TdxwebcomboBox = clase
Privado
FhtmlSelect: ihtmlselectelement;
función getCount: integer;
procedimiento setitemindex (valor const: entero);
función getItemIndex: Integer;
función getName: string;
procedimiento setName (const vals: string);
función getValue: string;
procedimiento setValue (valor const: string);
procedimiento setCombinterface (valor const: ihtmlselectelement);
función getItEmByName (elename: string): string;
función getItEmByIndex (índice: entero): string;
función getItemAttribute (index: integer; atribName: string): oleVariant;
público
constructor create (awebcombo: ihtmlselectelement);
procedimiento agregar (ele: ihtmlelement);
procedimiento insertar (ele: ihtmlelement; índice: entero);
procedimiento eliminar (índice: entero);
Propiedad Combinterface: ihtmlselectelement lea fhtmlselect escribir setcombinterface;
Recuento de propiedades: Integer Read GetCount;
Propiedad itemIndex: Integer Read getItemIndex Write setItemIndex;
Propiedad itemByIndex [índice: integer]: cadena leer getitembyindex;
Propiedad itemByName [elename: string]: string read getItEmByName;
Propiedad itemattribute [índice: integer; atribName: string]: olevariant read getItemattribute;
Nombre de la propiedad: cadena leer getName Write setName;
Valor de propiedad: cadena Leer getValue Write setValue;
fin;
Implementación
fin.
Unidad de implementación de código de la clase de análisis HTMLPARSER
Código
(****************************************************** *****)
(*Estudio dexiano*)
(*Biblioteca de la unidad de análisis HTML*)
(**)
(*Unidad dxhtmlParser*)
(*Copyright (c) 2008-2010 sin inactivo*)
(*Correo electrónico: [email protected]: 75492895*)
(****************************************************** *****)
unidad dxhtmlParser;
interfaz
Utiliza Windows, MSHTML, ActiveX, DXHTMLELEMENT, formularios;
tipo
Tdxhtmlparser = clase
Privado
Fhtmldoc: ihtmlDocument2;
Fhtml: cadena;
Fwebtables: tdxtableCollection;
Fwebelements: tdxwebelementCollection;
Fwebcomb: tdxwebcomboBox;
procedimiento sethtml (valor const: string);
función getWebComboBox (aname: string): tdxwebcomboBox;
público
constructor crear;
destructor destruir; anular;
propiedad html: cadena leer fhtml escribir sethtml;
Propiedad Webtables: TDxtableCollection lea fwebtables;
Propiedad Webelements: TDXWebelementCollection Leer fWebelements;
Propiedad WebComboBox [Nombre: String]: TDXWebComboBOX Read getWebComboBox;
fin;
Implementación
{Tdxhtmlparser}
constructor tdxhtmlparser.create;
Comenzar
Coinitializar (nulo);
// Crear interfaz IhtmlDocument2
CoCreateInstance (class_htmlDocument, nil, clsctx_inproc_server, iid_ihtmlDocument2, fhtmldoc);
Afirmar (fhtmldoc <> nil, 'construir htmlDocument interfaz fallado');
Fhtmldoc.set_designmode ('en');
mientras que no (fhtmldoc.readyState = 'complete') do
Comenzar
dormir (1);
Aplicación. ProcessMessages;
fin;
Fwebtables: = tdxtableCollection.create (fhtmldoc);
Fwebelements: = tdxwebelementCollection.create (nil);
Fwebcomb: = tdxwebcomboBox.create (nil);
fin;
destructor tdxhtmlparser.destroy;
Comenzar
FWebTables.Free;
Fwebelements.free;
FWebcomb.Free;
Couninicializar;
heredado;
fin;
función tdxhtmlparser.getWebComboBox (aname: string): tdxwebcomboBox;
Comenzar
if fWebelements.Collection <> nil entonces
Comenzar
Fwebcomb.combinterface: = fwebelements.elementByName [aname] como ihtmlselectelement;
Resultado: = fWebComb;
fin
el más resultado: = nil;
fin;
procedimiento tdxhtmlparser.sethtml (valor const: string);
Comenzar
Si fhtml <> valor entonces
Comenzar
Fhtml: = valor;
Fhtmldoc.body.innerhtml: = fhtml;
Fwebelements.collection: = fhtmldoc.all;
fin;
fin;
fin.