function GetHtml(const WebBrowser: TWebBrowser): string; const BufSize = $10000; var Size: Int64; Stream: IStream; hHTMLText: HGLOBAL; psi: IPersistStreamInit; begin if not Assigned(WebBrowser.Document) then Exit;
OleCheck(WebBrowser.Document.QueryInterface(IPersistStreamInit, psi)); try //OleCheck(psi.GetSizeMax(Size)); hHTMLText := GlobalAlloc(GPTR, BufSize); if 0 = hHTMLText then RaiseLastWin32Error;
function GetHtml(const WebBrowser:
TWebBrowser): string;
const
BufSize = $10000;
var
Size: Int64;
Stream: IStream;
hHTMLText: HGLOBAL;
psi: IPersistStreamInit;
begin
if not Assigned(WebBrowser.Document) then Exit;
OleCheck(WebBrowser.Document.QueryInterface(IPersistStreamInit, psi));
try
//OleCheck(psi.GetSizeMax(Size));
hHTMLText := GlobalAlloc(GPTR, BufSize);
if 0 = hHTMLText then RaiseLastWin32Error;
OleCheck(CreateStreamOnHGlobal(hHTMLText, True, Stream));
try
OleCheck(psi.Save(Stream, False));
Size := StrLen(PChar(hHTMLText));
SetLength(Result, Size);
CopyMemory(PChar(Result), Pointer(hHTMLText),
Size);
finally
Stream := nil;
end;
finally
psi := nil;
end;
end;
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, OleCtrls, SHDocVw, mshtml, ExtCtrls, DB, ADODB,
ComCtrls, ActiveX, ComObj;
memo1.Text:=GetHtml(WebBrowser1);
memo1.Lines.SaveToFile('888.txt');
执行下面这一句报错!!!
OleCheck(WebBrowser.Document.QueryInterface(IPersistStreamInit,psi));
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, OleCtrls, SHDocVw, StdCtrls, ExtCtrls,mshtml,ActiveX;type
TForm1 = class(TForm)
WebBrowser1: TWebBrowser;
ComboBox1: TComboBox;
ComboBox2: TComboBox;
Label1: TLabel;
Timer1: TTimer;
ComboBox3: TComboBox;
ComboBox4: TComboBox;
Memo1: TMemo;
Button2: TButton;
procedure FormShow(Sender: TObject);
procedure ComboBox2Change(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
procedure ComboBox4Change(Sender: TObject);
procedure ComboBox1Change(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;
URL:WideString;
i:integer;
implementation{$R *.dfm}
function GetHtml(const WebBrowser:TWebBrowser): string;
const
BufSize = $10000;
var
Size:Int64;
Stream:IStream;
hHTMLText: HGLOBAL;
psi:IPersistStreamInit;
begin
if not Assigned(WebBrowser.Document) then Exit;
OleCheck(WebBrowser.Document.QueryInterface(IPersistStreamInit,psi));
try
//OleCheck(psi.GetSizeMax(Size));
hHTMLText := GlobalAlloc(GPTR, BufSize);
if 0 = hHTMLText then RaiseLastWin32Error;
OleCheck(CreateStreamOnHGlobal(hHTMLText, True, Stream));
try
// OleCheck(psi.Save(Stream, False));
Size := StrLen(PChar(hHTMLText));
SetLength(Result, Size);
CopyMemory(PChar(Result), Pointer(hHTMLText),
Size);
finally
Stream := nil;
end;
finally
psi := nil;
end;
end;procedure TForm1.FormShow(Sender: TObject);
begin
if combobox2.Text<>'' then Timer1.Interval:=1000*strtoint(combobox2.Text);WebBrowser1.Top:=0;
WebBrowser1.Left:=0;
form1.Width:=screen.Width;
//form1.Height:=screen.Height;
WebBrowser1.Width:=screen.Width;
WebBrowser1.Height:=screen.Height;
URL := WideString('www.zgzcw.com/jqdc.asp?bflc='+combobox1.Text);// 2005046');
i:=strtoint(combobox1.Text);
//WebBrowser1.Navigate(URL);
WebBrowser1.Navigate(URL);end;procedure TForm1.ComboBox2Change(Sender: TObject);
begin
if combobox2.Text<>'' then Timer1.Interval:=1000*strtoint(combobox2.Text);
end;procedure TForm1.Timer1Timer(Sender: TObject);
beginif combobox3.Text='加' then i:=i+1 else i:=i-1;if (i<1000000) then //胜负
URL:='http://www.zgzcw.com/yhdc.asp?lc='+inttostr(i)
else
URL := WideString('www.zgzcw.com/jqdc.asp?bflc='+inttostr(i)); WebBrowser1.Navigate(URL);
if i=2004048 then i:=2005001;
combobox1.Text:=inttostr(i);
end;procedure TForm1.ComboBox4Change(Sender: TObject);
var
s:string;begin
if combobox4.Text='胜负' then begin
s:=copy(combobox1.Text,1,4)+copy(combobox1.Text,6,2);
i:=strtoint(s);
end else begin
s:=copy(combobox1.Text,1,4)+'0'+copy(combobox1.Text,5,2);
i:=strtoint(s);
end;
end;procedure TForm1.ComboBox1Change(Sender: TObject);
begin
if i=2004049 then i:=2005001;
i:=strtoint(combobox1.Text); if (i<1000000) then //胜负
URL:='http://www.zgzcw.com/yhdc.asp?lc='+inttostr(i)
else
URL := WideString('www.zgzcw.com/jqdc.asp?bflc='+inttostr(i)); WebBrowser1.Navigate(URL);
end;procedure TForm1.Button2Click(Sender: TObject);
begin
memo1.Text:=GetHtml(WebBrowser1);
memo1.Lines.SaveToFile('888.txt');
end;end.
Memo1.Lines.SaveToFile('888.txt');
2种方法都有需要注意的地方:
1.TWebBrowser,有貌似内存泄露的问题,如果程序自动在不断地打开各种网页,内存是比较容易被耗尽的,我没有找到真正的解决办法。2.IdHttp,如果程序自动在不断地打开各种网页,socket会被大量占用直至无可用。这个有解决办法,IdHttp的Request点开后,有个connection属性,填上close就阔以了。Request是对http协议中定义的报文头的实现,connection=close表示,通知server端,我发送的Request,在你处理完并Response后,请你来主动关闭连接。
IdHTTP在哪里可以找的到呀