使用 twebbrowser 从 delphi 的网站抓取图像
Scraping images from Website in delphi with twebbrowser
我正在尝试制作一个小工具,用于从访问的站点下载所有图像。它必须使用 twebbrowser 组件制作。我客户的测试站点是 Click。目前我使用 getelementbyid 选择图片,但有些图片没有 ID。我怎样才能找到失踪者?非常感谢
您可以 "walk" 文档并使用 WebDocument.all.item(itemnum,'').
查看每个元素,而不是通过 id 请求特定元素
var
cAllElements: IHTMLElementCollection;
eThisElement: IHTMLElement;
WebDocument: IHTMLDocument2;
=======
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 的元素 .tagName,或进行任何您需要的评估以确定它是否是图片并像以前一样处理它。
段
页面加载后,查询 TWebBrowser.Document
属性 以获取 IHTMLDocument2
interface, and then you can enumerate the elements of the IHTMLDocument2.images
集合:
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 <img>
标签中找到图像。如果您还需要在 <input type="image">
标签中查找图像,则必须枚举 IHTMLDocument2.all
collection looking for instances of the IHTMLInputElement
interface whose type
属性 的元素是 "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 组件制作。我客户的测试站点是 Click。目前我使用 getelementbyid 选择图片,但有些图片没有 ID。我怎样才能找到失踪者?非常感谢
您可以 "walk" 文档并使用 WebDocument.all.item(itemnum,'').
查看每个元素,而不是通过 id 请求特定元素var
cAllElements: IHTMLElementCollection;
eThisElement: IHTMLElement;
WebDocument: IHTMLDocument2;
=======
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 的元素 .tagName,或进行任何您需要的评估以确定它是否是图片并像以前一样处理它。
段
页面加载后,查询 TWebBrowser.Document
属性 以获取 IHTMLDocument2
interface, and then you can enumerate the elements of the IHTMLDocument2.images
集合:
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 <img>
标签中找到图像。如果您还需要在 <input type="image">
标签中查找图像,则必须枚举 IHTMLDocument2.all
collection looking for instances of the IHTMLInputElement
interface whose type
属性 的元素是 "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;