1、利用WebBrowser读取网页中表格的数据利用 WebBrowser 读取网页中表格的数据unit Unit1;interfaceusesWindows, Messages, SysUtils, Variants, Classes,Graphics, Controls, Forms,Dialogs, MSHTML, StdCtrls, OleCtrls, SHDocVw;typeTForm1 = class(TForm)Button1: TButton;WebBrowser1: TWebBrowser;procedure FormCreate(Sender: TObject);procedu
2、re Button1Click(Sender: TObject);private Private declarations public Public declarations end;varForm1: TForm1;implementation$R *.dfmfunctionGetHtmlTableCell(aTable: IHTMLTable; aRow, aCol:Integer): IHTMLElement;varRow: IHTMLTableRow;beginResult := nil;if aTable = nil then Exit;if aTable.rows = nil t
3、hen Exit;Row := aTable.rows.item(aRow, aRow) asIHTMLTableRow;if Row = nil then Exit;Result := Row.cells.item(aCol, aCol) as IHTMLElement;end;function GetHtmlTable(aDoc: IHTMLDocument2;aIndex: Integer): IHTMLTable;varlist: IHTMLElementCollection;beginResult := nil;if aDoc = nil then Exit;if aDoc.all
4、= nil then Exit;list := aDoc.all.tags(table) as IHTMLElementCollection;if list = nil then Exit;Result := list.item(aIndex, aIndex) as IHTMLTable;end;function GetWebBrowserHtmlTableCellText(constAWebBrowser: TWebBrowser;const TableIndex, RowIndex, ColIndex: Integer;var ResValue: string): Boolean;Doci
5、ntf: IHTMLDocument2;tblintf: IHTMLTable;node: IHTMLElement;beginResValue := docintf := AWebBrowser.Document asIHTMLDocument2;tblintf := GetHtmlTable(docintf, TableIndex);node := GetHtmlTableCell(tblintf, RowIndex, ColIndex);Result := node <> nil;if Result thenResValue := Trim(node.innerText);e
6、nd;function GetHtmlTableRowHtml(aTable: IHTMLTable;aRow: Integer): IHTMLElement;varRow: IHTMLTableRow;beginResult := nil;if aTable = nil then Exit;if aTable.rows = nil then Exit;Row := aTable.rows.item(aRow, aRow) asIHTMLTableRow;if Row = nil then Exit;Result := Row as IHTMLElement;end;function GetW
7、ebBrowserHtmlTableCellHtml(constAWebBrowser: TWebBrowser;const TableIndex, RowIndex, ColIndex: Integer;var ResValue: string): Boolean;varDocintf: IHTMLDocument2;tblintf: IHTMLTable;node: IHTMLElement;beginResValue := ;docintf := AWebBrowser.Document asIHTMLDocument2;tblintf := GetHtmlTable(docintf,
8、TableIndex);node := GetHtmlTableCell(tblintf, RowIndex, ColIndex);Result := node <> nil;if Result thenResValue := Trim(node.innerHTML);end;function GeHtmlTableHtml(aTable: IHTMLTable;aRow: Integer): IHTMLElement;Row: IHTMLTableRow;beginResult := nil;if aTable = nil then Exit;if aTable.rows = n
9、il then Exit;Row := aTable.rows.item(aRow, aRow) asIHTMLTableRow;if Row = nil then Exit;Result := Row as IHTMLElement;end;function GetWebBrowserHtmlTableHtml(constAWebBrowser: TWebBrowser;const TableIndex, RowIndex: Integer;var ResValue: string): Boolean;varDocintf: IHTMLDocument2;tblintf: IHTMLTabl
10、e;node: IHTMLElement;beginResValue := ;docintf := AWebBrowser.Document asIHTMLDocument2;tblintf := GetHtmlTable(docintf, TableIndex);node := GeHtmlTableHtml(tblintf, RowIndex);Result := node <> nil;if Result thenResValue := node.innerHtml;end;procedure TForm1.FormCreate(Sender: TObject);beginWebBrowser1.Navigate(H:ReadTable00.html);end;procedure TForm1.Button1Click(Sender: TObject);varResValue: string;beginif GetWebBrowserHtmlTableCellText(WebBrowser1, 0, 0, 0, ResValue) thenbeginShowMessage(ResValue);end elsebeginShowMessage( 指定的表格不存在 );end;end;end.
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1