利用WebBrowser读取网页中表格的数据
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
利用WebBrowser读取网页中表格的数据
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, MSHTML, StdCtrls, OleCtrls, SHDocVw;type TForm1 = class(TForm)
Button1: TButton;
WebBrowser1: TWebBrowser;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}function GetHtmlTableCell(aTable: IHTMLTable; aRow, aCol: Integer): IHTMLElement;
var
Row: IHTMLTableRow;begin
Result := nil;
if aTable = nil then Exit;
if aTable.rows = nil then Exit;
Row := aTable.rows.item(aRow, aRow) as IHTMLTableRow;
if Row = nil then Exit;
Result := Row.cells.item(aCol, aCol) as IHTMLElement; end;function GetHtmlTable(aDoc: IHTMLDocument2; aIndex: Integer): IHTMLTable;
var
list: IHTMLElementCollection;
begin
Result := nil;
if aDoc = nil then Exit;
if aDoc.all = 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(const AWebBrowser: TWebBrowser;
const TableIndex, RowIndex, ColIndex: Integer;
var ResValue: string): Boolean;
var
Docintf: IHTMLDocument2;
tblintf: IHTMLTable;
node: IHTMLElement;
begin
ResValue := ' ';
docintf := AWebBrowser.Document as IHTMLDocument2;
tblintf := GetHtmlTable(docintf, TableIndex);
node := GetHtmlTableCell(tblintf, RowIndex, ColIndex); Result := node <> nil;
if Result then
ResValue := Trim(node.innerText);
end;function GetHtmlTableRowHtml(aTable: IHTMLTable; aRow: Integer): IHTMLElement;
var
Row: IHTMLTableRow;
begin
Result := nil;
if aTable = nil then Exit;
if aTable.rows = nil then Exit;
Row := aTable.rows.item(aRow, aRow) as IHTMLTableRow;
if Row = nil then Exit;
Result := Row as IHTMLElement;
end;function GetWebBrowserHtmlTableCellHtml(const AWebBrowser: TWebBrowser;
const TableIndex, RowIndex, ColIndex: Integer;
var ResValue: string): Boolean;
var
Docintf: IHTMLDocument2;
tblintf: IHTMLTable;
node: IHTMLElement;
begin
ResValue := ' ';
docintf := AWebBrowser.Document as IHTMLDocument2;
tblintf := GetHtmlTable(docintf, TableIndex);
node := GetHtmlTableCell(tblintf, RowIndex, ColIndex); Result := node <> nil;
if Result then
ResValue := Trim(node.innerHTML);
end;function GeHtmlTableHtml(aTable: IHTMLTable; aRow: Integer): IHTMLElement;
var