我做过一个可以下载页面上所有图片的程序,内嵌ie,下面是取图的核心代码:
ieThis 是 IEThis: IWebbrowser2;
其实用的是IHtmlDocument2接口和IHtmlElementCollection接口
procedure TForm1.DownLoadBtnClick(Sender: TObject);
var
doc: IHTMLDocument2;
all,Img: IHTMLElementCollection;
len,i,j,flag: integer;
item: IHTMLElement;
vAttri: Variant;
ImgList: TStrings;
UrlStrM: TMemoryStream;
function GetAddrs(ImageList: TStrings): integer;
var
i: integer;
begin
Result := 0;
for i := 0 to ImagesForm.ImgsAddr.Items.Count-1 do
begin
if ImagesForm.ImgsAddr.Checked then
ImageList.Add(ImagesForm.ImgsAddr.Items);
end;
Result := ImageList.Count;
end;
begin
if Assigned(IEThis) then begin
Addrs.Clear;
doc := iethis.document as IHTMLDocument2;
all := doc.Get_all;
Img := Doc.Get_Images;
len := Img.get_length;
for i := 0 to len-1 do begin
item := Img.item(i,varempty) as IHTMLElement;
// if item.get_tagname = 'A' then
begin
flag := 0;
//VAttri := item.getAttribute('protocol',flag);
//if vAttri = 'http:' then
begin
vAttri := item.getAttribute('href',flag);
// if ExtractFileExt(Vattri)='.jpg' then
Addrs.Add(VAttri);
end;
end;
end;
ImagesForm := TImagesForm.Create(application);
try
ImagesForm.ImgsAddr.Items.Assign(Addrs);
//for j := 0 to ImagesForm.ImgsAddr.Count-1 do
// Imagesform.ImgsAddr.Checked[j] := true;
if ImagesForm.ShowModal = mrOK then
begin
UrlStrM := TMemoryStream.Create;
ImgList := TStringList.Create;
try
if GetAddrs(ImgList) = 0 then
begin
ShowMessage('Empty!');
exit;
end;
begin
//ÒÔÏÂÊÇÏÂÔصĴúÂë
for I := 0 to ImgList.count-1 do // Iterate
begin
UrlStrM.Clear;
IDHttp1.Get(ImgList,UrlStrM);
UrlStrM.SaveToFile(ImagesForm.Path.Text+'/'+ExtractUrlFileName(ImgList));
end; // for
//NMFTP1.Disconnect;
end;
finally
ImgList.Free;
end;
end;
finally
ImagesForm.Free;
end;
end;
end;