用贴子里带的html例子procedure TForm1.Button2Click(Sender: TObject); //uses MShtml; var doc: IHTMLDocument2; mhref: IHTMLAnchorElement; mElem: IHTMLElement; mElemColl: IHTMLElementCollection; mImg: IHTMLImgElement; i,j: integer; mDispatch: IDispatch; mstr: string; begin Memo1.Lines.Clear; doc := WebBrowser1.Document as IHTMLDocument2; for i:=0 to doc.all.length-1 do begin mDispatch := doc.all.item(i,0); if succeeded(mDispatch.QueryInterface(IHTMLAnchorElement,mhref)) then begin mstr := mhref.href + ';' + mhref.target + ';'; mElem := mhref as IHTMLElement; mstr := mstr + mElem.innerText; mDispatch := mElem.children; if succeeded(mDispatch.QueryInterface(IHTMLElementCollection,mElemColl)) then begin if mElemColl.length>0 then begin for j:=0 to mElemColl.length-1 do begin mDispatch := mElemColl.item(j,0); if succeeded(mDispatch.QueryInterface(IHTMLImgElement,mImg)) then mstr := mstr + ';imgsrc:' + mImg.src else mstr := mstr + mElem.innerHTML; //非IMG类型显示全部内容 end; end; end; Memo1.Lines.Add(mstr); end; end; end; 显示结果: http://www.zzz.com/;_blank;Test2 http://www.xxx.com/;_blank;Test;imgsrc:http://www.aaa.com/bbb.jpg http://www.yyy.com/;_blank;test3;imgsrc:http://www.aaa.com/bbb.jpg
//uses MShtml;
var
doc: IHTMLDocument2;
mhref: IHTMLAnchorElement;
mElem: IHTMLElement;
mElemColl: IHTMLElementCollection;
mImg: IHTMLImgElement;
i,j: integer;
mDispatch: IDispatch;
mstr: string;
begin
Memo1.Lines.Clear;
doc := WebBrowser1.Document as IHTMLDocument2;
for i:=0 to doc.all.length-1 do
begin
mDispatch := doc.all.item(i,0);
if succeeded(mDispatch.QueryInterface(IHTMLAnchorElement,mhref)) then
begin
mstr := mhref.href + ';' + mhref.target + ';';
mElem := mhref as IHTMLElement;
mstr := mstr + mElem.innerText;
mDispatch := mElem.children;
if succeeded(mDispatch.QueryInterface(IHTMLElementCollection,mElemColl)) then
begin
if mElemColl.length>0 then
begin
for j:=0 to mElemColl.length-1 do
begin
mDispatch := mElemColl.item(j,0);
if succeeded(mDispatch.QueryInterface(IHTMLImgElement,mImg)) then
mstr := mstr + ';imgsrc:' + mImg.src
else
mstr := mstr + mElem.innerHTML; //非IMG类型显示全部内容
end;
end;
end;
Memo1.Lines.Add(mstr);
end;
end;
end;
显示结果:
http://www.zzz.com/;_blank;Test2
http://www.xxx.com/;_blank;Test;imgsrc:http://www.aaa.com/bbb.jpg
http://www.yyy.com/;_blank;test3;imgsrc:http://www.aaa.com/bbb.jpg