我在工作中使用VBA自动化来获取票证系统的一些信息。我正试图将该值放入生成的表中,但唯一与工作表"Plan1"上的列"A"相关的信息是<td>
,它包含溢出:隐藏的CSS属性。我不知道它们是否相关,但巧合的是,它们是唯一没有出现的数据。有人能帮我吗?
HTML代码:
<div id="posicionamentoContent">
<table class="grid">
<thead>...</thead>
<tbody>
<tr id="937712" class="gridrow">
<td width="200px"> Leonardo Peixoto </td>
<td width="200px"> 23/12/2015 09:45 </td>
<td width="200px"> SIM </td>
<td width="200px"> Telhado da loja com pontos de vazamento.</td>
<td width="200px" align="center"></td>
<td width="200px" align="center"></td>
</tr>
...
...
...
完整代码:https://i.stack.imgur.com/4BsFo.png
我需要获得前4个<td>
文本(Leonardo Peikoto,2015年12月23日09:45,SIM和Telhado da loja com pontos de vazamento。)但它们只是我无法获得的文本。
Obs:当我使用开发人员工具(f12)检查每个元素时,它完美地向我展示了<td>
中所需的信息。但当我打开"源代码"页面检查html时,代码是这样的:
<div id="tabPosicionamento" style="padding: 5px 0 5px 0;" class="ui-tabs-hide">
div id="posicionamentoContent"></div>
</div>
VBA示例:
Sub extractTablesData1()
'we define the essential variables
Dim IE As Object, obj As Object
Dim ticket As String
Set IE = CreateObject("InternetExplorer.Application")
ticket= InputBox("Enter the ticket code")
With IE
.Visible = False
.navigate ("https://www.example.com/details/") & ticket
While IE.ReadyState <> 4
DoEvents
Wend
ThisWorkbook.Sheets("Plan1").Range("A1:K500").ClearContents
Set data = IE.document.getElementsByClassName("thead")(0).getElementsByTagName("td")
i = 0
For Each elemCollection In data
ThisWorkbook.Sheets("Plan1").Range("A" & i + 1) = data(i).innerText
i = i + 1
Next elemCollection
End With
IE.Quit
Set IE = Nothing
....
....
End Sub
此函数在工作表Plan1的列"A"中仅返回<td class=info3"></td>
和<td class=info4"></td>
,但我需要<td class=info1"></td>
和<td class=info2 also."></td>
由于代理阻止,我无法读取页面代码,但不久前我遇到了类似的问题,我找到的解决方案是将所有数据放在剪贴板上并粘贴。在那之后,我清理了表格上的数据。
这里是我用来做这件事的代码:
Set ieTable = ie.document.getElementById("ID")
If Not ieTable Is Nothing Then
Set clip = New DataObject
clip.SetText "<html>" & ieTable.outerHTML & "</html>"
clip.PutInClipboard
Sheet1.Range("A1").Select
ActiveSheet.PasteSpecial Format:="Unicode Text", link:=False, DisplayAsIcon:=False, NoHTMLFormatting:=True
End If
考虑到您需要隔离4条td行,您可以通过每次搜索的循环来实现这一点。
在您的示例中,它对数据进行计数,但不使用它。此外,单元格赋值应该是cells(x,y).value。这是工作代码。
Sub extractTablesData1()
'we define the essential variables
Dim IE As Object, Data As Object
Dim ticket As String
Set IE = CreateObject("InternetExplorer.Application")
With IE
.Visible = False
.navigate ("put your data url here")
While IE.ReadyState <> 4
DoEvents
Wend
Set Data = IE.document.getElementsByTagName("tr")(0).getElementsByTagName("td")
i = 1
For Each elemCollection In Data
ActiveWorkbook.Sheets(1).Cells(1, i).Value = elemCollection.innerHTML
i = i + 1
Next elemCollection
End With
IE.Quit
Set IE = Nothing
End Sub
它没有带来我需要的信息(持续<td>
)
<div id="posicionamentoContent">
<table class="grid">
<thead>...</thead>
<tbody>
<tr id="937712" class="gridrow">
<td width="200px"> Leonardo Peixoto </td>
<td width="200px"> 23/12/2015 09:45 </td>
<td width="200px"> SIM </td>
<td width="200px"> Telhado da loja com pontos de vazamento.</td>
<td width="200px" align="center"></td>
<td width="200px" align="center"></td>
</tr>