首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

利用WebBrowser读取网页中表格的数据(已有大部分代码,但读取失败),该如何处理

2012-06-15 
利用WebBrowser读取网页中表格的数据(已有大部分代码,但读取失败)比如这样一个网页:HTML codehtml xmlns

利用WebBrowser读取网页中表格的数据(已有大部分代码,但读取失败)
比如这样一个网页:

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.csdn.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;


  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
  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 GetWebBrowserHtmlTableHtml(const AWebBrowser: TWebBrowser;
  const TableIndex, RowIndex: Integer;
  var ResValue: string): Boolean;
var
  Docintf: IHTMLDocument2;
  tblintf: IHTMLTable;
  node: IHTMLElement;
begin
  ResValue := ' ';
  docintf := AWebBrowser.Document as IHTMLDocument2;
  tblintf := GetHtmlTable(docintf, TableIndex);
  node := GeHtmlTableHtml(tblintf, RowIndex);
  Result := node <> nil;
  if Result then
    ResValue := node.innerHtml;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  WebBrowser1.Navigate('H:\ReadTable\00.html');
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  ResValue: string;
begin
  if GetWebBrowserHtmlTableCellText(WebBrowser1, 0, 0, 0, ResValue) then
  begin
    ShowMessage(ResValue);
  end else
  begin
    ShowMessage('指定的表格不存在 ');
  end;
end;

end.




[解决办法]
list := aDoc.all.tags('table ') as IHTMLElementCollection;
改为
list := aDoc.all.tags('table') as IHTMLElementCollection;
去掉table后的空格

热点排行