【问题标题】:How to pull a text from a table but the table has the same name as other tables? VBA, excel如何从表格中提取文本但表格与其他表格具有相同的名称? VBA,优秀
【发布时间】:2016-10-06 15:58:27
【问题描述】:

我制作了一个代码,可以访问一个网站并提取他们的投资标准。但我只需要该表中的一个单元格,并且多个表的表类名称相同。

我需要获取 EBITDA,它在表格 class= cTblListBody 到目前为止,这是我的代码:

 Sub SearchBot()

 Dim objIE As InternetExplorer 
 Dim aEle As HTMLLinkElement 
 Dim y As Integer 
 Dim result As String 
 Dim TR As Object, TD As Object
 Dim tbl As Object, obj_tbl As Object

Set objIE = New InternetExplorer   
objIE.Visible = True 

objIE.navigate "https://website.com" 

Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop 

objIE.document.getElementById("SearchTopBar").Value = _
  Sheets("Sheet2").Range("A2").Value  
Set oNode = objIE.document.getElementsByClassName("iPadHack tmbsearchright")  
(0) 
oNode.Click
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop 

b = 2  

Dim tblEle
Set tblEle = objIE.document.getElementsByClassName("cTblListBody")(5)

    Sheets("Sheet2").Range("B" & b).Value = tblEle.innerText 
    Debug.Print tblEle.innerText

    b = b + 1
 Next     
objIE.Quit 'close the browser

结束子

完整的html代码:

html
<head id="ctl02_headcontrol">
<body>
<div id="gcontainer" style="z-index:200000" onmouseout="calendarTimeout();"
onmouseover="if (timeoutId) clearTimeout(timeoutId);"></div>
<style>
<script type="text/javascript">
<img id="_jsVersionedShim" title=""
src="https://w3.ciqimg.com/CIQDOTNET/images/shim.gif?urwvid=3502569" alt=""
style="display:none;">
<script type="text/javascript">
<link title="IQ" rel="search"
type="application/opensearchdescription+xml"
href="/ciqdotnet/search/autocompleteprovidergenerator.axd">
<link rel="stylesheet" type="text/css"
href="/CIQDOTNET/library/Combined/CIQHeader.css">
<style>
<div id="topBanner" style="width: 100%; height: 69px;">
<table style="height: 100%" width="100%" cellspacing="0" cellpadding="0"
border="0">
<tbody>
<tr valign="bottom">
<tr id="bodyrow" style="height:100%;" valign="top">
<td id="ll_leftBorder_mid">
<td id="ll_cont" class="ll_cont_ex">
<td id="leftPageBorder" style="width: 10px;">
<td style="width: 100%;">
<div id="contentArea">
<script type="text/javascript">
<script type="text/javascript">
<div id="UpdateProgressDiv" style="visibility:hidden;"></div>
<script type="text/javascript"
src="/CIQDotNet/Charting/Library/highstock.js">
<script type="text/javascript"
src="/CIQDotNet/CreditAnalytics/CIQCharts/themes/light.js">
<script type="text/javascript"
src="/CIQDotNet/CreditAnalytics/CIQCharts/lib/util.js">
<script type="text/javascript"
src="/CIQDotNet/CreditAnalytics/CIQCharts/CIQCharts.js">
<style type="text/css">
<script type="text/javascript"
src="/CIQDotNet/News/library/NewsAndBlogs.js">
<script type="text/javascript">
<form id="frmMain" method="post" action="./company.aspx?companyId=30995038">
<div class="aspNetHidden">
<script type="text/javascript">
<script src="/CIQDotNet/WebResource.axd?d=nZzj3YZngGCPUXbcooPdPCjPJSuAIp
DU_l-5lPsFAlauINflCZuBPW8NfeQFL1nsY13w4LY1&t=635802961220000000"
type="text/javascript">
<script language="javascript">
<script src="/CIQDotNet/LeftLinks/LeftLinksContent.aspx
 leftLinksHashKey=e1HX3w3nPsHpyHn6IEXdcA%3d%3d&urwvid=3502569"
 type="text/javascript">
<script type="text/javascript">
<script src="/CIQDotNet/library/CIQDotNet/Web/functional.js"
type="text/javascript">
<script type="text/javascript">
<script src="/CIQDotNet/ScriptResource.axd
d=BQRO1XhoYSDCQiunG6j3W6BEdFJUaLhgrsqvXzeFZkJH6K5tgvKMDrgFuDHlO
 1ymPMZRaduCK4dNstqE6toFrC4k8xUF9d5645" type="text/javascript">
<script src="/CIQDotNet/ScriptResource.axd?
<script src="Company/BubbleChart.asmx/js" type="text/javascript">
<script type="text/javascript">
<script src="/CIQDotNet/Research/Services/ResearchService.asmx/js"
type="text/javascript">
<div class="aspNetHidden">
<script type="text/javascript">
<div id="CompanyHeaderInfo" class="cPageTitle" style="margin-bottom:38px;">
<table class="cTblListBody" style="width:100%;" border="1">
<div style="width:100%;">
<div style="width:100%;">
<div style="width:100%;">
<div style="width:100%;">
<br class="tableSpacer">
<table style="width:100%;border-collapse:collapse;" cellspacing="0"
cellpadding="0">
<tbody>
<tr>
<td>
<table class="cTblHeaderBG" style="width:100%;">
<table class="cTblListBody" style="width:100%;border-collapse:collapse;"
cellspacing="0" cellpadding="2">
<a name="#ctl22$ctl09"></a>
<table class="cTblListBody" rules="all" cellspacing="1" cellpadding="2"
bordercolor="#d8dde1" border="1">
<tbody>
<tr class="cColHeaderBG">
<tr>
<tr style="background-color:#F9F9F9;">
<tr>
<tr style="background-color:#F9F9F9;">
<td>EBITDA</td>
<td style="width:50px;" align="right">
<span></span>
-
</td>
<td style="width:50px;" align="right">
<td style="width:50px;" align="right">
<td style="width:50px;" align="right">
</tr>
<tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</div>
<div style="width:100%;">
<div style="width:100%;">
<div style="width:100%;">
<div style="width:100%;">
<br class="tableSpacer">
<div> </div>
<script type="text/javascript">
<script type="text/javascript">
<script language="JavaScript">
<script language="JavaScript">
<script language="JavaScript">
<script type="text/javascript">
</form>
<table width="100%" cellspacing="0" cellpadding="0" border="0">
</div>
</td>
<td id="rightPageBorder" style="width: 10px;">
</tr>
<tr valign="top">
</tbody>
</table>
<div id="btmFooter" class="ftrBG">
<a id="_hotkey1" accesskey="1" tabindex="-1" onclick="HotKey(1);"
onfocus="HotKeyIE(1)" href="javascript:void(0);"
style="position:fixed;top:0;left:0"></a>
<a id="_hotkey2" accesskey="2" tabindex="-1" onclick="HotKey(2);"
onfocus="HotKeyIE(2)" href="javascript:void(0);"
style="position:fixed;top:1;left:0"></a>
<a id="_hotkey3" accesskey="3" tabindex="-1" onclick="HotKey(3);"
onfocus="HotKeyIE(3)" href="javascript:void(0);"
style="position:fixed;top:2;left:0"></a>
<a id="_hotkey4" accesskey="4" tabindex="-1" onclick="HotKey(4);"
onfocus="HotKeyIE(4)" href="javascript:void(0);"\
style="position:fixed;top:3;left:0"></a>
<a id="_hotkey5" accesskey="5" tabindex="-1" onclick="HotKey(5);"
onfocus="HotKeyIE(5)" href="javascript:void(0);"
style="position:fixed;top:4;left:0"></a>
<a id="_hotkey6" accesskey="6" tabindex="-1" onclick="HotKey(6);"
onfocus="HotKeyIE(6)" href="javascript:void(0);"
style="position:fixed;top:5;left:0"></a>
<a id="_hotkey7" accesskey="7" tabindex="-1" onclick="HotKey(7);"
onfocus="HotKeyIE(7)" href="javascript:void(0);"
style="position:fixed;top:6;left:0"></a>
<a id="_hotkey8" accesskey="8" tabindex="-1" onclick="HotKey(8);"
onfocus="HotKeyIE(8)" href="javascript:void(0);"
style="position:fixed;top:7;left:0"></a>
<a id="_hotkey9" accesskey="9" tabindex="-1" onclick="HotKey(9);"
onfocus="HotKeyIE(9)" href="javascript:void(0);"
style="position:fixed;top:8;left:0"></a>
<a id="_hotkey0" accesskey="0" tabindex="-1" onclick="HotKey(0);"
onfocus="HotKeyIE(0)" href="javascript:void(0);"
style="position:fixed;top:9;left:0"></a>
</body>
</html>

【问题讨论】:

    标签: excel vba datatable


    【解决方案1】:

    您需要获取该 ClassName 的 第三个​​ 实例,因此,不要循环匹配 ClassName 元素的集合,试试这个:

    Dim tblEle
    Set tblEle = objIE.document.getElementsByClassName("cTblListBody")(2)
    

    然后,我认为您可以从该元素中获取单元格值。

    另请注意:

    您使用变量i,但我没有看到在代码中的任何地方声明:For Each aEle In objIE.document.getElementsByClassName("cTblListBody").Rows. (i).Cells(1).innerText。这应该是b 而不是i

    【讨论】:

    • 好的,我使用第一个代码而不是 For Each aEleInobjIE.document。 getElementsByClassName ("cTblListBody").Rows.(i).Cells(1).innerText 我在看朋友的代码,并试图模仿相同的过程。
    • 那么我们将 dim tblEle 定义为什么?
    • Dim tblEle 隐式属于 Variant 类型,可以包含任何内容。你可能想做Dim tblEle as Object,但这没关系。
    • 我修改了代码。我需要正确的类名的第 5 个实例,而不是第 2 个。太好了,现在我需要循环单元格,以便它搜索单元格 A2:A1882。知道该怎么做吗?
    • @sameer 请将其作为一个单独的问题提出,并避免使用 cmets 提出一系列后续跟进问题——在您研究该主题之后。循环是一个基本的编程概念。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-02
    • 1970-01-01
    • 2014-01-06
    • 1970-01-01
    • 2021-06-27
    相关资源
    最近更新 更多