如何解析HTML文件?

fbiboss 2012-02-02 02:58:26
比如有个table,如何能根据ID找到它,然后读出有几个TR,每个TR中有几个TD,每个TD的内容,中间内容如果是类似这样的<font color="#999999">,那如何再解析有什么属性




<table id="xxxx" width="100%">
<tr>
<td bgcolor="#FFFFFF">登录用户:
</td>
</tr>
<tr>
<td bgcolor="#FFFFFF">用户类型:<font color="#999999">
注册会员
</font></td>
</tr>
</table>
...全文
331 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
showmehow 2012-10-27
  • 打赏
  • 举报
回复
什么情况!
lijin1230 2012-02-02
  • 打赏
  • 举报
回复

同4楼。
lzg827 2012-02-02
  • 打赏
  • 举报
回复
楼上的,WebBrowser控件用的很妙。
这是Delphi自带的控件,方便!
erhan 2012-02-02
  • 打赏
  • 举报
回复

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, OleCtrls, SHDocVw, StdCtrls, MSHtml;

type
TForm1 = class(TForm)
wb1: TWebBrowser;
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
wb1.Navigate('http://localhost:12880/mtm/abc.htm');
end;

procedure TForm1.Button1Click(Sender: TObject);
var
doc: IHTMLDocument2;
mTable: IHTMLTable;
mRow: IHTMLTableRow;
mCell: IHTMLElement;
mElement: IHTMLElementCollection;
i,j: integer;
begin
doc := wb1.Document as IHTMLDocument2;

mTable := doc.all.item('xxxx',0) as IHTMLTable;
for i:=0 to mTable.rows.length-1 do
begin
mRow := mTable.rows.item(i,0) as IHTMLTableRow;

for j:=0 to mRow.cells.length-1 do
begin
mCell := mRow.cells.item(j,0) as IHTMLElement;
showmessage(mCell.innerHTML);
end;
end;
end;

end.
lzg827 2012-02-02
  • 打赏
  • 举报
回复
贴过来算了,哪位兄弟找到控件实现一样的功能,顺便也学习下。



function RightPos(Substr, S: string): Integer;
begin
Result := Pos(ReverseString(Substr),ReverseString(S));
if Result > 0 then
Result := Length(S) - Result + 1 - Length(Substr) + 1;
end;


(*
#==========================================================
#功能: 返回两个字符串之间的字串
#参数: cStringStr 要截取的字符串
# StartStr 起始的字符串
# EndStr 截止的字符串
#返回: 字符型 截取后的字符串
#==========================================================
*)
function SubStr(cStringStr, StartStr, EndStr: string): string;
var
iStart, iEnd: Longint;
begin
//iStart := pos(StartStr, cStringStr);
iStart := RightPos(StartStr, cStringStr);

//返回指明字串StartStr在字串cStringStr中的iStartPosz之后的位置
if iStart = 0 then
begin
SubStr := ' ';
Exit;
end;

//返回指明字串EndStr在字串cStringStr中的位置
iEnd := Pos(EndStr, cStringStr);

if iEnd = iStart then
begin
SubStr := ' ';
Exit;
end;

//截取字串
if iEnd = 0 then
begin
SubStr := ' ';
exit;
end
else
SubStr := MidBStr(cStringStr, iStart + length(StartStr), iEnd - iStart - Length(StartStr));

end;

(*
#==========================================================
#功能: 返回两个字符串之间的字串
#参数: cStringStr 要截取的字符串
# StartStr 起始的字符串
# EndStr 截止的字符串
#返回: 字符型 截取后的字符串 (截取最右边EndStr,EndStr左边最近的StartStr,中间部分)
#==========================================================
*)
function SubStrExR(cStringStr, StartStr, EndStr: string): string;
var
iStart, iEnd: Longint;
TemStr:string;
begin
//返回指明字串EndStr在字串cStringStr中的位置
iEnd := Pos(EndStr, cStringStr);

TemStr := Copy(cStringStr,1,iEnd);

//iStart := pos(StartStr, cStringStr);
iStart := RightPos(StartStr, TemStr);

//返回指明字串StartStr在字串cStringStr中的iStartPosz之后的位置
if iStart = 0 then
begin
SubStrExR := ' ';
Exit;
end;

if iEnd = iStart then
begin
SubStrExR := ' ';
Exit;
end;

//截取字串
if iEnd = 0 then
begin
SubStrExR := ' ';
exit;
end
else
SubStrExR := MidBStr(cStringStr, iStart + length(StartStr), iEnd - iStart - Length(StartStr));

end;

(*
#==========================================================
#功能: 返回两个字符串之间的字串
#参数: cStringStr 要截取的字符串
# StartStr 起始的字符串
# EndStr 截止的字符串
#返回: 字符型 截取后的字符串 (截取最左边StartStr,StartStr右边最近的EndStr,中间部分)
#==========================================================
*)
function SubStrExL(cStringStr, StartStr, EndStr: string): string;
var
iStart, iEnd: Longint;
TemStr:string;
begin
iStart := pos(StartStr, cStringStr);

TemStr := copy(cStringStr,iStart+length(StartStr)+1,length(cStringStr)-(iStart+length(StartStr)));

//返回指明字串EndStr在字串cStringStr中的位置
iEnd := iStart+length(StartStr) + Pos(EndStr, TemStr);


//返回指明字串StartStr在字串cStringStr中的iStartPosz之后的位置
if iStart = 0 then
begin
SubStrExL := ' ';
Exit;
end;

if iEnd = iStart then
begin
SubStrExL := ' ';
Exit;
end;

//截取字串
if iEnd = 0 then
begin
SubStrExL := ' ';
exit;
end
else
SubStrExL := MidBStr(cStringStr, iStart + length(StartStr), iEnd - iStart - Length(StartStr));

end;

lzg827 2012-02-02
  • 打赏
  • 举报
回复
这类问题我一般用关键字截取,就那几个函数倒来倒去,很容易搞定的。
什么Pos,copy,MidBStr等
不知道有没有专门的控件解析HTML。

参考我在另外个帖子回复的代码,肯定能实现。
不太习惯因为一个功能点就加一个控件
http://topic.csdn.net/u/20120201/22/a4b09b8e-0464-4c09-b290-055ac291431e.html
喝口水 2012-02-02
  • 打赏
  • 举报
回复
使用xml可以很容易完成

5,388

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧