我正在尝试抓取一个网站,并且有一个元素,如果您将鼠标移到它上面,它会在气泡中显示一些信息。我正在使用VBA来抓取页面,但我不知道如何找到特定元素。
查看页面的来源,我得到了这个:
<td class="right odds up">
<div onmouseout="delayHideTip()" onmouseover="page.hist(this,'P-0.00-00','1q3cfx2rrhkx0x3jtah',381,event,0,1)"
提供一些细节,以下是我想要抓取的页面:match page.当您将鼠标移到箭头上时,数字会出现一个包含某些内容的矩形。这就是我想要的。
我的代码:
Private Sub CommandButton6_Click()
the_start:
Dim objIE As Object
Set objIE = CreateObject("InternetExplorer.Application")
objIE.Visible = True
On Error Resume Next
objIE.Navigate ("http://www.oddsportal.com/baseball/usa/mlb-2014/arizona-diamondbacks-st-louis-cardinals-jeOoAP9r/")
Do
DoEvents
If Err.Number <> 0 Then
objIE.Quit
Set objIE = Nothing
GoTo the_start:
End If
Loop Until objIE.ReadyState = 4
objIE.Document.all.Item
Dim js As Variant
js = page.hist(this,P-0.00-0-0,1q3cfx2rrhkx0x3jtah,381,event,0,1) ???
Call objIE.Document.parentWindow.execScript(js, "JavaScript") ???
MsgBox "objIE.js"
End Sub
答案 0 :(得分:1)
考虑一下这个例子:
Option Explicit
Sub Test()
Dim objIE, colTdNodes, i, objTdNode, objDivNode, strTooltipContent, objDispNode
' open the page
Set objIE = CreateObject("InternetExplorer.Application")
With objIE
.Visible = True
.Navigate "http://www.oddsportal.com/baseball/usa/mlb-2014/arizona-diamondbacks-st-louis-cardinals-jeOoAP9r/"
' wait until IE and the page are ready
Do While .Busy Or Not .readyState = 4: DoEvents: Loop
' wait until the DOM is ready
Do Until .document.readyState = "complete": DoEvents: Loop
' wait until the table is ready
Do While TypeName(.document.getElementById("odds-data-table")) = "Null": DoEvents: Loop
End With
' below is an example how to retrieve tooltips content
' get table target cells nodes collection
Set colTdNodes = objIE.document.getElementsByClassName("right odds")
' loop through each cell in collection
For i = 0 To colTdNodes.Length - 1
' choose the cell from collection
Set objTdNode = colTdNodes(i)
' get div node from the cell
Set objDivNode = objTdNode.ChildNodes.Item(0)
' get the tooltip content
strTooltipContent = GetTooltipContent(objDivNode)
' create new div node to display tooltip content
Set objDispNode = objIE.document.createElement("div")
' add the created node into the cell
objTdNode.appendChild objDispNode
' set id and style
objDispNode.ID = "tooltip" & i
objDispNode.Style.Background = "#ddd"
objDispNode.Style.padding = "5px"
objDispNode.Style.margin = "5px"
' display the tooltip content in the node
objDispNode.innerHtml = strTooltipContent
Next
' hide the last tooltip
objIE.document.parentWindow.execScript "delayHideTip();", "javascript"
End Sub
Function GetTooltipContent(objNode)
Dim objEventMouseOver, objTipNode, objDocument
' get document object
Set objDocument = objNode.OwnerDocument
' create mouse event object
Set objEventMouseOver = objDocument.createEvent("MouseEvents")
' setup mouseover event
objEventMouseOver.initMouseEvent "mouseover", True, True, objDocument.parentWindow, 1, 12, 345, 7, 220, False, False, True, False, 0, ""
' send mouseover event to the div node
' support for dispatchEvent was added in IE9
objNode.dispatchEvent objEventMouseOver
' retrieve appeared tooltip node
Set objTipNode = objDocument.getElementById("tooltiptext")
' get tooltip html content
GetTooltipContent = objTipNode.innerHtml
End Function