uses UrlMon;function DownloadFile(SourceFile, DestFile: string): Boolean; begin try Result := UrlDownloadToFile(nil, PChar(SourceFile), PChar(DestFile), 0, nil) = 0; except Result := False; end; end; procedure TForm1.Button1Click(Sender: TObject); var i,j: Integer; Source,Dest,ext: OleVariant; begin WebBrowser1.Navigate('http://www.xxx.com'); while WebBrowser1.ReadyState < READYSTATE_COMPLETE do Application.ProcessMessages; if WebBrowser1.OleObject.Document.all.tags('A').Length = 0 then Exit; Memo1.Clear; for i := 0 to WebBrowser1.OleObject.Document.all.tags('A').Length - 1 do begin Source := WebBrowser1.OleObject.Document.all.tags('A').Item(i); j := LastDelimiter('.', Source); ext := UpperCase(Copy(Source, j+1, Length(Source))); if (ext = 'RAR') or (ext = 'ZIP') then begin Memo1.Lines.Add(Source.innerText + ': ' + Source.href); Dest := ExtractFilePath(ParamStr(0)) + Source.innerText; DownloadFile(Source, Dest); end; end; end;説明:用「TWebBrowser」控件、下载网页中<a href="...">
begin
try
Result := UrlDownloadToFile(nil, PChar(SourceFile), PChar(DestFile), 0, nil) = 0;
except
Result := False;
end;
end; procedure TForm1.Button1Click(Sender: TObject);
var
i,j: Integer;
Source,Dest,ext: OleVariant;
begin
WebBrowser1.Navigate('http://www.xxx.com');
while WebBrowser1.ReadyState < READYSTATE_COMPLETE do
Application.ProcessMessages; if WebBrowser1.OleObject.Document.all.tags('A').Length = 0 then Exit;
Memo1.Clear; for i := 0 to WebBrowser1.OleObject.Document.all.tags('A').Length - 1 do
begin
Source := WebBrowser1.OleObject.Document.all.tags('A').Item(i);
j := LastDelimiter('.', Source);
ext := UpperCase(Copy(Source, j+1, Length(Source))); if (ext = 'RAR') or (ext = 'ZIP') then
begin
Memo1.Lines.Add(Source.innerText + ': ' + Source.href);
Dest := ExtractFilePath(ParamStr(0)) + Source.innerText;
DownloadFile(Source, Dest);
end;
end;
end;説明:用「TWebBrowser」控件、下载网页中<a href="...">