成功實現使用delphi獲取百度的驗證碼圖片(只獲得圖片,不進行識別,識別之後再研究吧O(∩_∩)O)
[delphi]
procedure TForm1.btn1Click(Sender: TObject);
procedure doSomething(ms: TMemoryStream);
var
Buffer:Word;
AjpgFile: TJPEGImage;
begin
ms.Position := 0;
if ms.Size = 0 then
Exit;
ms.ReadBuffer(Buffer,2); //讀取文件的前2個字節,放到Buffer裡面
if Buffer=$4D42 then //如果前兩個字節是以4D42[低位到高位]
begin
ShowMessage('BMP'); //那麼這個是BMP格式的文件
end
else if Buffer=$D8FF then //如果前兩個字節是以D8FF[低位到高位]
begin
ShowMessage('JPEG'); //........一樣 下面不注釋了
ms.Position := 0;
AjpgFile := TJPEGImage.Create;
AjpgFile.LoadFromStream(ms);
Image1.Picture.Graphic := AjpgFile;
end
else if Buffer=$4947 then
begin
ShowMessage('GIF');
end
else if Buffer=$5089 then
begin
ShowMessage('PNG');
end;
end;
var
Openurl : string;
elem: IHTMLElement;
coll: IHTMLElementCollection;
i: Integer;
url, Text: string;
d2,D:IHTMLDocument2;
d1:IHTMLDocument;
e:IHTMLElement;
e2:IHTMLElement2;
cp: IHTMLControlRange;
img:IHTMLImgElement;
ce:IHTMLControlElement;
bmp:TBitmap ;
r0:TRect;
newbmp:TBitmap;
r1:TRect;
checkstr:string;
MyHandle :THandle ;
bmpPtr:Pointer;
ms:TMemoryStream; //內存流對象
begin
ms := TMemoryStream.Create; //建立內存流對象
IdHTTPBaiDu := TIdHTTP.Create(nil);
IdHTTPBaiDu.ReadTimeout := 2000000;
IdHTTPBaiDu.IOHandler := IdSSLIOHandlerSocketOpenSSL1.Create(nil);
Openurl := 'https://passport.baidu.com/v2/?reg&u=http://www.baidu.com/&tpl=mn';
// Openurl := 'http://zc.qq.com/chs/index.html';
// Openurl := 'http://www.doubao.com/user/reg';//豆包網
try
BaiDuWebBrowser.Navigate(Openurl);
while BaiDuWebBrowser.Busy do
begin
Application.ProcessMessages;
end;
BaiDuWebBrowser.Stop;
if BaiDuWebBrowser.Document = nil then
Exit;
//獲取源代碼
D := BaiDuWebBrowser.Document as IHTMLDocument2;
e :=d.body as IHTMLElement;
e2 :=e as IHTMLElement2;
cp :=e2.createControlRange as IHTMLControlRange;
d2 :=BaiDuWebBrowser.Document as IHTMLDocument2;
//下面是破解驗證碼
coll := d.all;
coll :=(coll.tags('img') as IHTMLElementCollection);
for i := 0 to coll.Length - 1 do
begin
//循環取出每個url
elem := (coll.item(i,0) as IHTMLElement);
url :=Trim(string(elem.getAttribute(WideString('src'), 0)));
//Text := Trim(string(elem.outertext));
if Pos('doubao' , url) > 0 then //豆包網
begin
if Pos('auth/checkcode',url) > 0 then
begin
IdHTTPBaiDu.Get(url,ms);
doSomething(ms);
Break;
end
end
else if Pos('qq.com' , url) > 0 then //騰訊QQ
begin
if pos('getimage', url) > 0 then
begin
IdHTTPBaiDu.Get(url,ms);
doSomething(ms);
Break;
end
end
else if Pos('baidu.com' , url) > 0 then //百度
begin
if Pos('genimage',url) > 0then
begin
IdHTTPBaiDu.Get(url,ms);
doSomething(ms);
Break;
end;
end;
end;
finally
end;
end;