procedure TForm1.GetCaptcha(wb:TWebBrowser);
var
doc:IHTMLDocument3;
ele:IHTMLElement;
rang:IHTMLControlRange;
s:string;
Bitmap:TBitmap;
begin
try
//获取src为s,其中captchaImg为图片ID
s := (IHTMLDocument2(wb.Document).images.item('captchaImg',EmptyParam) as IHTMLElement).getAttribute('src',0);
Bitmap:=TBitmap.Create; //尤其需要注意这一点
rang:=((IHTMLDocument2(wb.Document).body as HTMLBody).createControlRange)as
IHTMLControlRange;
rang.add(IHTMLDocument2(wb.Document).images.item('captchaImg',EmptyParam)as
IHTMLControlElement);
rang.execCommand('Copy',False,0);
if Clipboard.HasFormat(CF_BITMAP) then
Bitmap.Assign(Clipboard);
Bitmap.SaveToFile('c:\\33.jpeg'); //记得是jpeg不是jpg,但是这两者的区别还不是很明白(虽然说jpg也能够下载成功)
except
end;
end;
这种方式下载下来的验证码为什么比IDHTTP下载下来的验证码要大?
利用Idhttp下载验证码:
url_p := 'https://login.10086.cn/captchazh.htm?type=05';
mStream := TMemoryStream.Create;
try
try
IdHTTP.Get(url_p, mStream);
mStream.SaveToFile(PicPath+'\'+inttostr(i)+'.jpeg');
except
end;
finally
mStream.Free;