2017-06-21 25 views
1

私は完全に困惑しています。私は、VBAのInternet Explorerオブジェクトを使ってGoogle画像検索のHTMLを解析しています。 (申し訳ありませんが、フォーマットする方法がわからない)HTML要素の背景の解析スタイルがありません

HTML image

があります

<a href="/imgres?imgurl=...&amp;imgrefurl=...&amp;docid=...&amp;tbnid=...&amp;vet=...&amp;w=1366&amp;h=768&amp;bih=638&amp;biw=1366&amp;q=cats&amp;ved=...;iact=mrc&amp;uact=8" jsaction="fire.ivg_o;mouseover:str.hmov;mouseout:str.hmou" class="rg_l" rel="noopener" style="background: rgb(200, 190, 194); width: 270px; height: 168px; left: 0px;"><img class="rg_ic rg_i" data-sz="f" name="z7O-qKoPKHzyaM:" alt="Image result for cat's" jsaction="load:str.tbn" onload="google.aft&amp;&amp;google.aft(this)" src="data:image/jpeg;base64,/9j/4AAQ..." style="width: 300px; height: 168px; margin-left: -15px; margin-right: -15px; margin-top: 0px;"><div class="_aOd rg_ilm"><div class="rg_ilmbg"><span class="rg_ilmn"> 1366&nbsp;×&nbsp;768 - wallpapercave.com </span></div></div></a> 

や絵のように:私はChromeのツールを内蔵した要素を検査するとき、私はこのようなものとしてHTMLを取得しますスタイルセット内の「バックグラウンド」値を取得したいと思います。しかし、私はそれをどこでも見つけることができません。 Chromeの検査ツールを使用してプロパティを表示すると、見ることのできる[スタイル]オプションはありません。 InnerHTMLには、スタイル要素の一部は含まれていませんが、「背景」は含まれていません。 そして、何が起こっているVBA

HTMLelement.getAttribute("Background") = "" 
HTMLelement.Style.Background = "" 
HTMLelement.Style.BackgroundColor = "" 

を通過、なぜ私はウェブページインスペクタを使用したときに、背景のプロパティを参照してください、しかし、上記の手段を介してアクセスすることはできませんか?

+0

を使用し、これは私が推測しているVBAアプリケーション(ありますオブジェクト指向ラングに普遍的ないくつかの方法を使用年齢)。要約すると、 'objIE'という名前の' InternetExplorer.Application'をgoogleイメージページに移動し、ロードするのを待ちます。 'set elem = objIE.document.getElementById(" rg_s ")。getElementsByTagName(" IMG " )(0).ParentElement' 'elem'は私の質問で参照している' IHTMLElement'です(もっと多くの行に広げましたが、それはアイデアです) – Greedo

答えて

0

この目的のために、anchor要素のgetAttribute("style")メソッドを使用できます。次に、styleIHTMLStyleであり、の文字列プロパティを持つの色です。 HTH

Option Explicit 

' Add reference to Microsoft Internet Controls (SHDocVw) 
' Add reference to Microsoft HTML Object Library 

Const url As String = "your-url-here" 

Sub GetInlineStyle() 
    Dim ie As SHDocVw.InternetExplorer 
    Dim doc As MSHTML.HTMLDocument 

    Debug.Assert Trim(url) <> "" 
    Set ie = New SHDocVw.InternetExplorer 
    ie.Visible = True 
    ie.navigate url 

    While ie.Busy Or ie.readyState <> READYSTATE_COMPLETE 
     DoEvents 
    Wend 

    Set doc = ie.document 

    ' Get target anchor element which contains the background color 
    Dim anchor As HTMLAnchorElement 
    Set anchor = doc.querySelector("a[class=rg_l]") 

    ' Use getAttribute("style") to get style of anchor 
    Dim style As IHTMLStyle 
    Set style = anchor.getAttribute("style") 

    ' IHTMLStyle has string property backgroundColor 
    Dim bgrColor As String 
    bgrColor = style.backgroundColor 
    Debug.Print bgrColor 

    ie.Quit 
End Sub 

出力

rgb(200, 190, 194) 

は、HTMLがGustafGunérませんJSを使用して、@

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 

<head> 
<!-- saved from url=(0016)http://localhost --> 
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" /> 
<title>Stackoverflow Greedo</title> 
</head> 

<body> 
    <div class="content"> 
     <a href="https://stackoverflow.com" class="rg_l" rel="noopener" 
      style="background: rgb(200, 190, 194); width: 270px; height: 168px; left: 0px"> 
      Stackoverflow Greedo  
     </a> 
    </div> 
</body> 

</html> 
+0

'' style.backgroundColor' 'this'(https://www.google.co.uk/search?q=stack+overflow&tbm=isch)画像検索を使用します。アンカーはページの正しい要素を指していますが、私はChromeの代わりにIEインスペクタを使用しており、 'background'プロパティの痕跡が見つかりませんでした。これは2つの推測につながります。まず、このプロパティはクロムに固有のもので、2番目のajaxスクリプトは画像がロードされた後にIEのプロパティを削除します。私はあなたがIEでデータを取得する方法を考えることができない限り、セレンを使用する必要がありますか? – Greedo

+0

@Greedoリンクが提供されているので、私はあなたの問題を完全にundestandできる:)。しかし、あなたが書いたように、IEではそのようなインラインスタイルは存在しません。だから、 'style.backgroundColor'の結果は正しいです、私はそれで何もできません:(。 – dee

関連する問題