缺少HTML元素背景中的分析样式
我完全被难住了。我正在使用VBA的InternetExplorer对象解析google图像搜索的HTML。当我使用Chrome的内置工具检查元素时,我得到的HTML如下所示:缺少HTML元素背景中的分析样式,html,css,vba,Html,Css,Vba,我完全被难住了。我正在使用VBA的InternetExplorer对象解析google图像搜索的HTML。当我使用Chrome的内置工具检查元素时,我得到的HTML如下所示: <a href="/imgres?imgurl=...&imgrefurl=...&docid=...&tbnid=...&vet=...&w=1366&h=768&bih=638&biw=
<a href="/imgres?imgurl=...&imgrefurl=...&docid=...&tbnid=...&vet=...&w=1366&h=768&bih=638&biw=1366&q=cats&ved=...;iact=mrc&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&&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 × 768 - wallpapercave.com </span></div></div></a>
发生了什么事,为什么在使用网页检查器时可以看到背景属性,但不能通过上述方式访问它?为此,可以使用目标
锚定元素的getAttribute(“style”)
方法。然后,样式
的类型为IHTMLStyle
,其背景色具有字符串属性。嗯
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
使用的HTML
斯塔克·格里多
为此,可以使用目标锚定
元素的getAttribute(“样式”)
方法。然后,样式
的类型为IHTMLStyle
,其背景色具有字符串属性。嗯
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
使用的HTML
斯塔克·格里多
@GustafGunér没有使用JS,这是一个VBA应用程序(我猜它使用了一些面向对象语言通用的方法)。作为总结,我将名为objIE
的InternetExplorer.Application
导航到google图像页面,等待加载,然后:Set elem=objIE.document.getElementById(“rg_s”).getElementsByTagName(“IMG”)(0)。ParentElement
其中elem
是我在问题中提到的IHTMLElement
(我把它扩展到更多的行中,但这就是想法)@GustafGunér没有使用JS,这是一个VBA应用程序(我猜它使用了一些面向对象语言通用的方法)作为总结,我将一个名为objIE
的InternetExplorer.Application
导航到一个谷歌图片页面,等待它加载,然后:Set elem=objIE.document.getElementById(“rg_s”).getElementsByTagName(“IMG”)(0)。ParentElement
其中elem
是我在问题中提到的IHTMLElement
(我把它分散在更多的行上,但这就是想法)嗯,返回“对于使用图像搜索时的style.backgroundColor
,即使锚定
指向页面上的正确元素。我使用了IE inspector而不是Chrome inspector,并且没有发现background
属性的痕迹。这导致了两种猜测;第一种猜测是Chrome独有的属性,第二种猜测是加载图像后,某些ajax脚本会删除IE中的属性。除非您能想到如何在IE中获取数据,否则我可能不得不使用Selenium?@Greedo现在通过提供的链接,我可以完全理解您的问题:).但正如您所写,IE中没有这种内联样式。因此样式.backgroundColor
的结果是正确的,我对此无能为力:(.Hmmm,返回“”对于使用图像搜索时的style.backgroundColor
,即使锚定
指向页面上的正确元素。我使用了IE inspector而不是Chrome inspector,并且没有发现background
属性的痕迹。这导致了两种猜测;第一种猜测是Chrome独有的属性,第二种猜测是加载图像后,某些ajax脚本会删除IE中的属性。除非您能想到如何在IE中获取数据,否则我可能不得不使用Selenium?@Greedo现在通过提供的链接,我可以完全理解您的问题:)但是正如你所写的,在IE中没有这样的内联样式。因此,样式.backgroundColor
的结果是正确的,我对此无能为力:(。
rgb(200, 190, 194)
<!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>