Recentemente, simulamos a página da web e retorno ao código -fonte da web e, em seguida, obtenha os elementos correspondentes na página da web Pesquisando, encontrei várias versões Delphi das classes HTMLPARSER. Por isso, pensei que, se houver um navegador na interface, podemos operar elementos da página da web através da interface do documento do WebBrowser, o que é muito conveniente! No entanto, se você simular o envio da página da web, o WebBrowser não aparece necessariamente na interface. Objeto dentro, ele pode realizar o HTML depois de analisá -lo, pesquisando o MSDN e, em seguida, no Google, ele funciona, e o método de construção é o seguinte:
// Crie a interface IHTMLDocument2
CoCreateInstance (classe_htmldocument, nil, clsctx_inproc_server, iid_ihtmldocument2, fhtmldoc);
Depois que a interface é criada, os elementos do documento podem ser analisados, o que é muito refrescante!
Combinando minhas próprias operações exclusivas, encapsulei alguns elementos da página da web, como combinação, tabela, quadro etc. e implementei um htmlparser.
Apenas uma declaração é dada aqui, faça o download do código no final
Código
(***************************************************** *****)
(*Estúdio dexiano*)
(*Biblioteca da classe de operação do elemento da página da web*)
(**)
(*DXHTMLELEMENT Unit*)
(*Copyright (c) 2008-2010 sem marcha lenta*)
(*E -mail: [email protected]: 75492895*)
(***************************************************** *****)
unidade dxhtmlelement;
interface
Usa Windows, Sysutils, ClipBrd, MSHTML, ActiveX, Oletrls, Graphics, Typinfo;
{Obtenha o tipo de elemento}
Função ISSELECTELEMENT (ALTEELEMENT: IHTMLELEMENT): booleano;
Função ISPWDELEMENT (ALTEELEMENT: IHTMLELEMENT): booleano;
função istextElement (elemento: ihtmlelement): boolean;
função istableElement (elemento: ihtmlelement): boolean;
função isElementCollection (elemento: ihtmlelement): boolean;
função ischelement (elemento: ihtmlelement): boolean;
função isradiobtnellement (elemento: ihtmlelement): boolean;
função ismemoElement (elemento: ihtmlelement): boolean;
função isformElement (elemento: ihtmlelement): boolean;
função isimgElement (elemento: ihtmlelement): boolean;
função isinimgElement (elemento: ihtmlelement): boolean;
função islabelement (elemento: ihtmlelement): boolean;
função islinkElement (elemento: ihtmlelement): boolean;
função islistElement (elemento: ihtmlelement): boolean;
função iscontrolElement (elemento: ihtmlelement): boolean;
função isobjectElement (elemento: ihtmlelement): boolean;
função isFrameElement (elemento: ihtmlelement): boolean;
função isInputbtnelement (elemento: ihtmlelement): boolean;
função isinHiddenElement (elemento: ihtmlelement): boolean;
função ISSUBMITELEMENT (elemento: ihtmlelement): boolean;
{Obtenha dados imgElement}
function getpicIndex (doc: ihtmldocument2; src: string; alt: string): integer;
função getpicElement (doc: ihtmldocument2; imggname: string; src: string; alt: string): ihtmlimgElement;
função getRegCodepic (doc: ihtmldocument2; imgnhame: string; src: string; alt: string): tpicture;
função getRegCodepic (Doc: ihtmldocument2; índice: Inteiro): tpicture;
função getRegCodepic (doc: ihtmldocument2; elemento: ihtmlimgElement): tpicture; sobrecarga;
tipo
TObjectfromlResult = função (lResult: lResult; const IID: tiid; wparam: wparam; out pobject): 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_inhdenden);
função getElementType (elemento: ihtmlelement): TelementType;
function getElementTypeName (elemento: ihtmlelement): string;
função gethtmltableCell (atable: ihtmltable; arow, acol: integer): ihtmlelement;
função gethtmltable (adoc: ihtmldocument2; aindex: inteiro): ihtmlTable;
função getwebbrowSerHtmltabLECELLTEXT (doc: ihtmldocument2;
const tableIndex, RowIndex, Colindex: Inteiro; var resvalue: string): boolean;
função gethtmltablerowhtml (atable: ihtmltable; arow: integer): ihtmlelement;
função getwebbrowSerHtmltableCelHtml (doc: ihtmldocument2;
const tableIndex, RowIndex, Colindex: Inteiro; var resvalue: string): boolean;
função gehtmltablehtml (atable: ihtmltable; arow: integer): ihtmlelement;
função getwebbrowSerHtmlTableHtml (doc: ihtmldocument2;
const tableIndex, RowIndex: Inteiro; var resvalue: string): boolean;
tipo
TdxwebfRameCollection = classe;
TdxWEBELEEMENTCOLLECTION = Class;
TloadState = (doc_loading, doc_completed, doc_invalidate);
Tdxwebframe = classe
Privado
Fframe: ihtmlwindow2;
FELEMENTCOLEÇÕES: TDXWEBELEMENTCOLECÇÃO;
Fwebframecollections: tdxwebframecollection;
função getsrc: string;
função getElementCount: Inteiro;
função getwebframecollections: tdxwebframecollection;
função getElementCollections: tdxwebElementCollection;
função getDocument: ihtmldocument2;
função getReadState: tloadState;
função getisloaded: boolean;
Procedimento setFrame (Valor const: ihtmlWindow2);
função getName: string;
público
Construtor Create (iframe: ihtmlwindow2);
Destructor Destruir; substituir;
Quadro da propriedade: ihtmlwindow2 leia fframe write setFrame;
Propriedade src: string leia getsrc;
Documento da propriedade: ihtmldocument2 leia getDocument;
Nome da propriedade: String leia getName;
quadros de propriedade: tdxwebframecollection leia getwebframecollections;
Propriedade ElementCount: Inteiro Read GetElementCount;
Propriedade ElementCollections: TDXWEBELEMENTCOLECÇÃO LEIA GetElementCollections;
Propriedade ReadyState: TloadState Read GetReadState;
Propriedade é carregada: boolean leia getisload;
fim;
TdxwebfRameCollection = classe
Privado
FfRameCollection: ihtmlframescollection2;
Quadro: tdxwebframe;
função getCount: integer;
função getFrameInterfaceByIndex (índice: inteiro): ihtmlWindow2;
função getFrameInterfaceByName (nome: string): ihtmlwindow2;
função getFrameByIndex (índice: número inteiro): tdxwebframe;
função getFrameByName (nome: string): tdxwebframe;
Procedimento setFrameCollection (Valor const: ihtmlframescollection2);
público
Construtor Create (Acollection: ihtmlframesCollection2);
Destructor Destruir; substituir;
Propriedade FRAMECOLLECÇÃO: IHTMLFRAMESCOLLECTION2 LEIA FFRAMECOLLECTION WRITE SetFramecollection;
Contagem de propriedades: Inteiro Read GetCount;
Propriedade FrameInterfaceByIndex [Índice: Inteiro]: ihtmlWindow2 Leia getFrameInterfaceByIndex;
Propriedade FrameInterfaceByName [Nome: String]: IHTMLWindow2 Leia getFrameInterfaceByName;
Propriedade FrameByIndex [Índice: Inteiro]: TDXWebFRame leia getFrameByIndex;
Propriedade FramebyName [Nome: String]: TDXWebFrame Read getFrameByName;
fim;
TdxWEBELEEMENTCOLLECTION = Class
Privado
FCollection: ihtmlelementCollection;
FCHILDCOLLECTION: TDXWEBELEMENTCOLECÇÃO;
função getCollection (índice: string): tdxwebElementCollection;
função getCount: integer;
função getElement (itemName: string; index: integer): ihtmlelement;
function getElementByName (itemName: string): ihtmlelement;
função getElementByIndex (índice: inteiro): ihtmlelement;
Procedimento setCollection (Valor const: ihtmlelementCollection);
público
Construtor Create (Acollection: ihtmlelementCollection);
Destructor Destruir; substituir;
Coleção de propriedades: ihtmlelementcollection leia fCollection write setCollection;
Propriedade ChildElementCollection [Índice: String]: TDXWEBELEMENTOLECTRION LEAD getCollection;
Propriedade ElementCount: Inteiro Read GetCount;
Elemento da propriedade [ItemName: String; Índice: Inteiro]: ihtmlelement leia getElement;
Propriedade ElementByName [ItemName: String]: ihtmlelement leia getElementByName;
Propriedade ElementByIndex [Índice: Inteiro]: ihtmlelement leia getElementByIndex;
fim;
TlinkCollection = classe (TDXWEBELEEMENTCOLECÇÃO)
fim;
Tdxwebtable = classe;
TdxtableCollection = classe
Privado
FtableCollection: ihtmlelementCollection;
Fdocument: ihtmldocument2;
Fwebtable: tdxwebtable;
function getTableInterfaceByName (aname: string): ihtmltable;
Procedimento SetDocument (Valor: IHTMLDocument2);
function getTableInterfaceByIndex (índice: inteiro): ihtmlTable;
função getCount: integer;
função getTableByIndex (índice: inteiro): tdxWebtable;
função getTableByName (aname: string): tdxwebtable;
público
Construtor CREATE (DOC: IHTMLDOCUMENT2);
Destructor Destruir; substituir;
Property tableInterfaceByName [aname: string]: ihtmltable read getTableInterfaceByName;
Property tableInterfaceByIndex [Índice: Inteiro]: ihtmlTable Read getTableInterfaceByIndex;
Propriedade TabelaByName [aname: string]: tdxwebtable read getTableByName;
Propriedade TabelaByIndex [Índice: Inteiro]: TDXWebtable Read GetTableByIndex;
Documento da propriedade: ihtmldocument2 leia fdocument write setDocument;
Contagem de propriedades: Inteiro Read GetCount;
fim;
TdxWebtable = classe
Privado
FtableInterface: ihtmlTable;
função getRowCount: Inteiro;
Procedimento SettableInterface (Valor const: ihtmlTable);
função getCell (acol, arow: integer): string;
função getRowColCount (RowIndex: Integer): Inteiro;
função getinnerhtml: string;
função getinnerText: string;
função getCelElement (acol, arow: integer): ihtmltableCell;
público
Construtor CREATE (ATABLE: IHTMLTABLE);
Propriedade TableInterface: ihtmltable leia ftableInterface Write SettableInterface;
Propriedade RowCount: Inteiro Read GetRowCount;
Célula de Propriedade [ACOL: Inteiro; AROW: Inteiro]: String leia getCell;
Propriedade CellElement [ACOL: Integer; AROW: Integer]: ihtmltableCell leia getCelElement;
Propriedade RowColCount [RowIndex: Integer]: Inteiro Read GetRowColCount;
Propriedade INNERHTML: String leia getinnerhtml;
Propriedade InnerText: String Read getinnerText;
fim;
TdxwebcomboBox = classe
Privado
FhtmlSelect: ihtmlselectElement;
função getCount: integer;
Procedimento setItemIndex (Valor const: Inteiro);
função getItemIndex: Inteiro;
função getName: string;
Procedimento setName (const valor: string);
função getValue: string;
Procedimento setValue (Valor const: string);
Procedimento setCombinterface (Valor const: ihtmlselectElement);
função getItembyName (elename: string): string;
função getItembyIndex (índice: número inteiro): string;
function getItemattribute (índice: número inteiro; attribName: string): olevariant;
público
Construtor Create (AWEBCOMBO: IHTMLElectElement);
Procedimento Add (ELE: IHTMLELEMENT);
Inserção do procedimento (ele: ihtmlelement; índice: número inteiro);
procedimento remover (índice: número inteiro);
Propriedade CombInterface: ihtmlselectElement leia fhtmlselect write setCombinterface;
Contagem de propriedades: Inteiro Read GetCount;
Propriedade ItemIndex: Inteiro Read GetItemIndex Write setItemIndex;
Propriedade ItemByIndex [Índice: Inteiro]: String leia getItembyIndex;
Propriedade ItemByName [Elename: String]: String leia getItembyName;
Propriedade ItemAttribute [Índice: Inteiro; AttribName: String]: Olevariant leia getItemattribute;
Nome da propriedade: string leia getName write setName;
Valor da propriedade: string leia getValue write setSetValue;
fim;
Implementação
fim.
Unidade de implementação de código da classe de análise HTMLPARSER
Código
(***************************************************** *****)
(*Estúdio dexiano*)
(*Biblioteca de unidade de análise HTML*)
(**)
(*Unidade dxhtmlparser*)
(*Copyright (c) 2008-2010 sem marcha lenta*)
(*E -mail: [email protected]: 75492895*)
(***************************************************** *****)
unidade dxhtmlparser;
interface
Usa Windows, MSHTML, ActiveX, DXHTMLELEMENT, FORMAS;
tipo
Tdxhtmlparser = classe
Privado
Fhtmldoc: ihtmldocument2;
Fhtml: string;
Fwebables: tdxtableCollection;
FWEBELENTES: TDXWEBELEMENTCOLECÇÃO;
Fwebcomb: tdxwebcomboBox;
procedimento sethtml (const valor: string);
função getwebcomboBox (aname: string): tdxwebcomboBox;
público
construtor Criar;
Destructor Destruir; substituir;
Propriedade html: string leia fhtml write sethtml;
Propriedade WebTables: tdxtableCollection leia fwebables;
Propriedade WebElements: TDXWEBELEMENTCOLLECTION LEIT FWEBELENTES;
Propriedade webcombobox [nome: string]: tdxwebcomboBox leia getwebcomboBox;
fim;
Implementação
{Tdxhtmlparser}
construtor tdxhtmlparser.create;
Começar
Coinitializar (nulo);
// Crie a interface IHTMLDocument2
CoCreateInstance (classe_htmldocument, nil, clsctx_inproc_server, iid_ihtmldocument2, fhtmldoc);
Assert (fhtmldoc <> nil, 'construir interface htmldocument falhou');
Fhtmldoc.set_designmode ('on');
enquanto não (fhtmldoc.readyState = 'complete') do
Começar
sono (1);
Application.processMessages;
fim;
Fwebtables: = tdxtableCollection.create (fhtmldoc);
FWEBELENTES: = TDXWEBELEMENTCOLLECTION.CREATE (NIL);
Fwebcomb: = tdxwebcomboBox.create (nil);
fim;
destruidor tdxhtmlparser.Destroy;
Começar
Fwebtables.free;
FWEBELEENTES.FREE;
Fwebcomb.free;
Counitialize;
herdado;
fim;
função tdxhtmlparser.getwebcomboBox (aname: string): tdxwebcomboBox;
Começar
Se fweblements.collection <> nil então
Começar
Fwebcomb.combinterface: = fwebElements.ElementByName [aname] como ihtmlselectElement;
Resultado: = fwebcomb;
fim
outro resultado: = nil;
fim;
procedimento tdxhtmlparser.sethtml (const valor: string);
Começar
se fhtml <> valor então
Começar
Fhtml: = value;
Fhtmldoc.body.innerhtml: = fhtml;
FwebElements.Collection: = fhtmldoc.all;
fim;
fim;
fim.