我的代码可以得到整个HTML源代码。不过上面的有点问题,呵呵。掉了i的声明,重新写:
function GetHTML(Document:IHTMLDocument2):String;
var i:integer
IDisp:IDispatch;
pElement:IHTMLElement;
begin
for I := 0 to Document.all.length - 1 do // Iterate
begin
IDisp:=Document.all.item(i,i);
IDisp.QueryInterface(IID_IHTMLElement,pElement);
if CompareText(pElement.tagName,'HTML')=0 then
begin
Result :=pElement.outerHTML;
exit;
end;
end; // for
end;
function GetHTML(Document:IHTMLDocument2):String;
var IDisp:IDispatch;
pElement:IHTMLElement;
begin
for I := 0 to Document.all.length - 1 do // Iterate
begin
IDisp:=Document.all.item(i,i);
IDisp.QueryInterface(IID_IHTMLElement,pElement);
if CompareText(pElement.tagName,'HTML')=0 then
begin
Result :=pElement.outerHTML;
exit;
end;
end; // for
end;
function WB_GetHTMLCode(WebBrowser: TWebBrowser; ACode: TStrings): Boolean;
var
ps: IPersistStreamInit;
ss: TStringStream;
sa: IStream;
s: string;
begin
result:=false;
ps := WebBrowser.Document as IPersistStreamInit;
s := '';
ss := TStringStream.Create(s);
try
sa := TStreamAdapter.Create(ss, soReference) as IStream;
try
Result := Succeeded(ps.Save(sa, True));
except
end;
if Result then ACode.Add(ss.Datastring);
finally
ss.Free;
end;
end;
//WB1.Navigate('http://cn.yahoo.com');
procedure TForm1.Button3Click(Sender: TObject);
begin
if WB_GetHTMLCODE(WB1,memo1.Lines) then ....
end;