利用WebBrowser读取网页中表格的数据(已有大部分代码,但读取失败),该如何处理
利用WebBrowser读取网页中表格的数据(已有大部分代码,但读取失败)
比如这样一个网页:
我想读取里面的数据,利用下面一段代码,但一直提示“找不到表格”
附代码的出处:http://topic.****.net/t/20060920/09/5033996.html
比如这样一个网页:
- HTML code
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>无标题文档</title> </head> <body> <table class="zhangdan_table" width="100%"> <tbody> <tr class="table_top"> <td width="16%">编号</td> <td width="19%">班别</td> <td width="10%">年龄 </td> <td width="25%">姓名 </td> <td width="30%">出生年月 </td> </tr> <tr class="table_li"> <td><b>001</b> </td> <td>一(1)</td> <td><span>¥20</span> </td> <td>小明</td> <td>2000-08-01 </td> </tr> </tbody> </table> </body> </html>
我想读取里面的数据,利用下面一段代码,但一直提示“找不到表格”
附代码的出处:http://topic.****.net/t/20060920/09/5033996.html
- Delphi(Pascal) code
unit Unit1;
interface
uses
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;