Image 用twebbrowser在delphi中从网站上抓取图像

Image 用twebbrowser在delphi中从网站上抓取图像,image,delphi,twebbrowser,Image,Delphi,Twebbrowser,我正在尝试制作一个小工具,从访问的站点下载所有图像。它必须由粗花纤维组件制成。我的客户提供的测试站点是。目前,我正在使用getelementbyid选择图片,但有些图片没有id。我如何处理缺少的图片?非常感谢,您可以使用WebDocument.all.item(itemnum“”)来“漫游”文档并查看每个元素,而不是按id请求特定元素 ======= cAllElements:=WebDocument.All For iThisElement:=0 to cAllElements.num-

我正在尝试制作一个小工具,从访问的站点下载所有图像。它必须由粗花纤维组件制成。我的客户提供的测试站点是。目前,我正在使用getelementbyid选择图片,但有些图片没有id。我如何处理缺少的图片?非常感谢,您可以使用WebDocument.all.item(itemnum“”)来“漫游”文档并查看每个元素,而不是按id请求特定元素

=======

 cAllElements:=WebDocument.All
  For iThisElement:=0 to cAllElements.num-1 do
    begin
      eThisElement:=cAllElements.item(iThisElement,'') as IHTMLElement;
      // check out eThisElement and do what you want
    end;
然后,您可以查看IMG的element.tagName,或者进行任何需要的评估,以确定它是否是图片,并像以前一样处理它


Dan

您可以使用WebDocument.all.item(itemnum.)“遍历”文档并查看每个元素,而不是按id请求特定元素

=======

 cAllElements:=WebDocument.All
  For iThisElement:=0 to cAllElements.num-1 do
    begin
      eThisElement:=cAllElements.item(iThisElement,'') as IHTMLElement;
      // check out eThisElement and do what you want
    end;
然后,您可以查看IMG的element.tagName,或者进行任何需要的评估,以确定它是否是图片,并像以前一样处理它


Dan

加载页面后,查询界面的
TWebBrowser.Document
属性,然后可以枚举集合的元素:

var
  Document: IHTMLDocument2;
  Images: IHTMLElementCollection;
  Image: IHTMLImgElement;
  I: Integer;
begin
  Document := WebBrowser1.Document as IHTMLDocument2;
  Images := Document.images;
  For I := 0 to Images.length - 1 do
  begin
    Image := Images.item(I, '') as IHTMLImgElement;
    // use Image as needed...
  end;
end;
请注意,这将仅在HTML
标记中找到图像。如果还需要在
标记中查找图像,则必须枚举集合中的元素,以查找属性为
“image”
的接口实例,例如:

var
  Document: IHTMLDocument2;
  Elements: IHTMLElementCollection;
  Element: IHTMLElement;
  Image: IHTMLImgElement;
  Input: IHTMLInputElement;
  I: Integer;
begin
  Document := WebBrowser1.Document as IHTMLDocument2;
  Elements := Document.all;
  For I := 0 to Elements.length - 1 do
  begin
    Element := Elements.item(I, '') as IHTMLElement;
    if Element is IHTMLImgElement then begin
      Image := Element as IHTMLImgElement;
      // use Image as needed...
    end
    else if Element is IHTMLInputElement then begin
      Input := Element as IHTMLInputElement;
      if Input.type = 'image' then
      begin
        // use Input as needed...
      end;
    end;
  end;
end;

加载页面后,查询界面的
TWebBrowser.Document
属性,然后可以枚举集合的元素:

var
  Document: IHTMLDocument2;
  Images: IHTMLElementCollection;
  Image: IHTMLImgElement;
  I: Integer;
begin
  Document := WebBrowser1.Document as IHTMLDocument2;
  Images := Document.images;
  For I := 0 to Images.length - 1 do
  begin
    Image := Images.item(I, '') as IHTMLImgElement;
    // use Image as needed...
  end;
end;
请注意,这将仅在HTML
标记中找到图像。如果还需要在
标记中查找图像,则必须枚举集合中的元素,以查找属性为
“image”
的接口实例,例如:

var
  Document: IHTMLDocument2;
  Elements: IHTMLElementCollection;
  Element: IHTMLElement;
  Image: IHTMLImgElement;
  Input: IHTMLInputElement;
  I: Integer;
begin
  Document := WebBrowser1.Document as IHTMLDocument2;
  Elements := Document.all;
  For I := 0 to Elements.length - 1 do
  begin
    Element := Elements.item(I, '') as IHTMLElement;
    if Element is IHTMLImgElement then begin
      Image := Element as IHTMLImgElement;
      // use Image as needed...
    end
    else if Element is IHTMLInputElement then begin
      Input := Element as IHTMLInputElement;
      if Input.type = 'image' then
      begin
        // use Input as needed...
      end;
    end;
  end;
end;

在dom中查找图像在dom中查找图像漫游文档的
图像
集合比遍历
所有
集合更直接。我同意。我忘了收集图像。这两种方法都可以,但是使用图像集合会更简单。.all集合对于特别搜索更为有用。浏览文档的
图像
集合比浏览
all
集合更为直接。我同意。我忘了收集图像。这两种方法都可以,但是使用图像集合会更简单。.all集合对于特别搜索更有用。