从一个HTML返回所有的图片链接

 
uses mshtml, ActiveX, COMObj, IdHTTP, idURI;

{ .... }

procedure GetImageLinks(AURL: string; AList: TStrings);
var
   IDoc: IHTMLDocument2;
   strHTML: string;
   v: Variant;
   x: Integer;
   ovLinks: OleVariant;
   DocURL: string;
   URI: TidURI;
   ImgURL: string;
   idHTTP: TidHTTP;
begin
   AList.Clear;
   URI := TidURI.Create(AURL);
   try
     DocURL := ’http://’ + URI.Host;
     if URI.Path <> ’/’ then
       DocURL := DocURL + URI.Path;
   finally
     URI.Free;
   end;
   Idoc := CreateComObject(Class_HTMLDocument) as IHTMLDocument2;
   try
     IDoc.designMode := ’on’;
     while IDoc.readyState <> ’complete’ do
       Application.ProcessMessages;
     v       := VarArrayCreate([0, 0], VarVariant);
     idHTTP := TidHTTP.Create(nil);
     try
       strHTML := idHTTP.Get(AURL);
     finally
       idHTTP.Free;
     end;
     v[0] := strHTML;
     IDoc.Write(PSafeArray(System.TVarData(v).VArray));
     IDoc.designMode := ’off’;
     while IDoc.readyState <> ’complete’ do
       Application.ProcessMessages;
     ovLinks := IDoc.all.tags(’IMG’);
     if ovLinks.Length > 0 then
     begin
       for x := 0 to ovLinks.Length - 1 do
       begin
         ImgURL := ovLinks.Item(x).src;
         // The stuff below will probably need a little tweaking
         // Deteriming and turning realtive URLs into absolute URLs
         // is not that difficult but this is all I could come up with
         // in such a short notice.
         if (ImgURL[1] = ’/’) then
         begin
           // more than likely a relative URL so
           // append the DocURL
           ImgURL := DocURL + ImgUrl;
         end
         else
         begin
           if (Copy(ImgURL, 1, 11) = ’about:blank’) then
           begin
             ImgURL := DocURL + Copy(ImgUrl, 12, Length(ImgURL));
           end;
         end;
         AList.Add(ImgURL);
       end;
     end;
   finally
     IDoc := nil;
   end;
end;


// Beispiel:
// Example:
procedure TForm1.Button1Click(Sender: TObject);
begin
   GetImageLinks(’http://www.swissdelphicenter.ch’, Memo1.Lines);
end;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值