2016-05-23 20 views
0

imは訪問したサイトからすべての画像をダウンロードする小さなツールを作ろうとしています。 twebbrowserコンポーネントで作成する必要があります。私の顧客のテストサイトはClickです。現時点では、imはgetelementbyidで写真を選択していますが、写真のいくつかはIDを持っていません。どのように私は行方不明のものに対処できますか?ありがとうalottwebbrowserでdelphiのウェブサイトから画像を掻き集める

+1

作業DOMを通じて探し画像 –

答えて

3

ページはIHTMLDocument2インタフェースのためのTWebBrowser.Documentプロパティを照会、ロードされ、その後、あなたが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">タグで画像を検索する必要がある場合は、type財産例えば、"image"あるIHTMLInputElementインターフェースのインスタンスを探してIHTMLDocument2.allコレクションの要素を列挙しなければなりません。

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; 
0

idで特定の要素を要求する代わりに、WebDocument.all.item(itemnum、 '')を使用して文書を「ウォーク」して各要素を表示できます。

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を見て、またはあなたはそれが絵であるかどうかを判断して処理するために必要なものは何でも評価するだろう以前と同じように

ダン

+0

文書の 'images'コレクションを歩くことは、' all'コレクションを歩くよりも簡単です。 –

+0

私は同意します。私は画像コレクションを忘れていた。いずれもうまくいくが、画像コレクションを使用する方がより簡単だろう。 .allコレクションは、アドホック検索にもっと役立ちます。 –

関連する問題