delphi HTML document接口获取网页中所有图片


本文整理自网络,侵删。

 


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;

相关阅读 >>

Delphi弹出信息框大全

Delphi datasnap(midas)三层架构中,常用事件及其触发顺序

Delphi 判断目录是否存在,不存在则创建目录并打开,存在则直接打开

Delphi vclzip实现分卷压缩

Delphi datasnap 初步入门使用总结

Delphi fileopendialog1 多选加载大量文件,不受中文文件名影响

Delphi listbox1列表随机打乱

Delphi中使用低层钩子 屏蔽win、ctrl+esc、alt+tab、alt+f4

Delphi xe listview导出excel

Delphi 的链式代码

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...