使用VBA从HTML表复制/粘贴,粘贴到Excel

时间:2015-06-06 23:37:04

标签: vba excel-vba excel

我在Excel中使用VBA。我希望使用VBA只复制HTML表格中的某些数据。我正在使用的表格如下:

<table class="RatingsTable standard" id="RatingsTable1">
                <tr>
                    <th class="top_header" colspan="16">General & Fielding Ratings</th>
                </tr>
                <tr>
                    <th class="event">Event</th><th class="season">Season</th><th class="height">Height</th><th class="weight">Weight</th><th class="rating overall" title="Overall"><span class="hidden">OV</span></th><th class="rating range" title="Range"><span class="hidden">RA</span></th><th class="rating glove" title="Glove"><span class="hidden">GL</span></th><th class="rating armstrength" title="Arm Strength"><span class="hidden">AS</span></th><th class="rating armaccuracy" title="Arm Accuracy"><span class="hidden">AA</span></th><th class="rating pitchcalling" title="Pitch Calling"><span class="hidden">PC</span></th><th class="rating durability" title="Durability"><span class="hidden">DU</span></th><th class="rating health" title="Health"><span class="hidden">HE</span></th><th class="rating speed" title="Speed"><span class="hidden">SP</span></th><th class="rating patience" title="Patience"><span class="hidden">PA</span></th><th class="rating temper" title="Temper"><span class="hidden">TP</span></th><th class="rating makeup" title="Makeup"><span class="hidden">MK</span></th>
                </tr>

                <tr class="odd">
                    <td class="event">Current</td><td class="season">36</td><td class="height">6-0</td><td class="weight">224</td><td>87</td><td>29</td><td>10</td><td>85</td><td>46</td><td>22</td><td>25</td><td>93</td><td>16</td><td>55</td><td>36</td><td>80</td>
                </tr>

                <tr class="even">
                    <td class="event">Projected</td><td class="season">-</td><td class="height">?</td><td class="weight">?</td><td>?</td><td>?</td><td>?</td><td>?</td><td>?</td><td>?</td><td>?</td><td>?</td><td>?</td><td>?</td><td>?</td><td>?</td>
                </tr>

                <tr class="odd">
                    <td class="event">Spring Training</td><td class="season">36</td><td class="height">6-0</td><td class="weight">224</td><td>87</td><td>29</td><td>10</td><td>85</td><td>46</td><td>22</td><td>25</td><td>93</td><td>16</td><td>55</td><td>36</td><td>80</td>
                </tr>

            </table>

我希望复制和粘贴的数据是此部分:

<td class="event">Current</td><td class="season">36</td><td class="height">6-0</td><td class="weight">224</td><td>87</td><td>29</td><td>10</td><td>85</td><td>46</td><td>22</td><td>25</td><td>93</td><td>16</td><td>55</td><td>36</td><td>80</td>

所以,我需要从这个特定的玩家桌子上复制36,6-0,224,87,29,10,55,46,22,25,93,16,55,36和80但我无法抓住这个特定的数据。有人能帮忙吗?

3 个答案:

答案 0 :(得分:0)

在Excel菜单(2007/2010或更高版本)中选择“Before: 4 stop mon-fri Chinese death Radbruch-Platz operator Min:30 apologized cooperate 4 stop mon-fri government computers WASHINGTON suspected Min:15 Chinese hackers After: 4 stop mon-fri Min:30 death Radbruch-Platz operator apologized cooperate 4 stop mon-fri Min:15 WASHINGTON suspected Chinese hackers ”标签,然后选择“Data”,然后输入网址并使用该箭头图标突出显示感兴趣的HTML文档表格,然后在Excel工作表中指定目标单元格。

您可以使用Macro Recorder生成模板VBA From Web,然后根据您的特定目的对其进行微调。 Microsoft文章中详细记录了过程:https://support.office.com/en-nz/article/Get-external-data-from-a-Web-page-708f2249-9569-4ff9-a8a4-7ee5f1b1cfba(它还描述了在Excel中创建Web查询的方法,您可以使用)。

根据您的意见添加:为了减少与业务逻辑相关的表的大小,如果外部网站为您提供此选项,则可以创建参数化的自定义Web查询。您最通用的解决方案是使用最适合您目标的Web数据填充Excel工作表,然后(根据需要)使用Excel VBA执行最终数据修剪。

就像FYI :还有一种下载/解析整个HTML文件的技术,但我不推荐这种方法。

希望这可能会有所帮助。最好的问候,

答案 1 :(得分:0)

我可以给你一个更精确的方法。您希望能够从表的各个部分中进行选择。

您可以看到,所追寻的是表中tr中的最后一个id="RatingsTable1"。该表的最后一行。

我们可以使用CSS选择器来访问它,以描述该位置。

#RatingsTable1 tr:last-child

上面说的元素ID为tr内的元素的最后一个子标签RatingsTable1

同样有first-childnth-child选择器。


CSS查询:

CSS query


VBA:

您通过querySelector的{​​{1}}方法应用它

您没有显示任何代码,但说您正在使用ie,那么它将是

document

如果您有html文档变量,例如htmlDoc,那么它将是:

ie.document.querySelector("#RatingsTable1 tr:last-child").innerText

答案 2 :(得分:0)

这个问题发布已经有一段时间了,但是由于我最近一直在从事类似的项目,所以我认为我可以贡献自己的解决方案。下面的方法演示了如何使用VBA解析HTML表的一般逻辑,可以对其进行修改以适合任何类似项目的需要。为了使以下功能正常工作,您需要引用MS HTML对象库。

Public Function parseTableHTML(stringHTML As String, tableID As String, rowClass As String)
    Dim sampleHTML As New MSHTML.HTMLDocument 'create an HTMLDocument object
    Dim tableHTML As HTMLTable
    Dim rowHTML As HTMLTableRow
    Dim cellHTML As HTMLTableCell
    sampleHTML.body.innerHTML = stringHTML 'set the HTMLDocument's body equal to the html code you want to parse
    Set tableHTML = sampleHTML.getElementById(tableID) 'get the element whose ID is equal to tableID (in this case the element you're interested in, is a table with tableID="RatingsTable1")
    Set rowHTML = tableHTML.getElementsByClassName(rowClass)(0) 'get the first row from the collection of rows that belong to the table of interest and their class name is rowClass (in this case rowClass="odd")
    For Each cellHTML In rowHTML.Cells 'loop through the cells that belong to the row of interest
        Debug.Print cellHTML.innerText
    Next cellHTML
End Function

按照相同的逻辑,如果感兴趣的表没有ID,但是您知道html代码段中有多个表,并且您对第一个表感兴趣,则可以从的集合中获取它。表格:

Set tableHTML = sampleHTML.getElementsByTagName("table")(0)

相同的原理适用于感兴趣的行,在这种情况下,您可以从行集合中获得以下信息:

Set rowHTML = tableHTML.getElementsByTagName("tr")(2)