把一张桌子从一堆HTML中抽出来高强 [英] Pulling a table out of a mess of HTML w/ VBA & Excel

查看:102
本文介绍了把一张桌子从一堆HTML中抽出来高强的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

'get the table based on the table’s id
Set ieDoc = ieApp.Document
Set ieTable = ieDoc.all.Item("contentBody")

'copy the tables html to the clipboard and paste to teh sheet
If Not ieTable Is Nothing Then
    Set clip = New DataObject
    clip.SetText "<table>" & ieTable.innerHTML & "</table>"
    clip.PutInClipboard
    Sheet1.Select
    Sheet1.Range("A1").Select
    Sheet1.PasteSpecial "Unicode Text"
End If

我已经玩过上面的代码,知道有更好的方法。 contentBody包括:

I've played with the code above and know there's a better way. the "contentBody" includes:

<div id="contentBody">
<div id="breadcrumb_navigation">
<ul class="nav nav-pills" style="margin-bottom: 0px;">
<h2 class="leftShim">Base Statistics</h2>
<div style="margin: 0px 15px 15px;">
<table class="table table-striped">
</div>
<br/>
<br/>
</div>

所有我想要的是,表< table class =table table-stripe>

但是由于我缺乏知识,我不知道从哪里开始修剪超量或者我应该尝试另一种方法。

But because of my lack of knowledge I'm not sure where to start trimming out the excess or if I should try another approach.

对于这个例子,来自网站的导航,搜索和面包屑被拉入我的工作表。在使用HTML标签之前,我已经抓住了数据,但只是数据,而不是整个表,而我目前在这里只是耸耸肩。

For this example the navigation, search and breadcrumb from the website gets pulled into my worksheet. I've grabbed data before using HTML tags, but only pieces of data and never an entire table and I'm currently left here just shrugging.

推荐答案

基本上可以通过标签名称获取项目,如本链接中所述: http://www.vbaexpress.com/forum/showthread.php?t=31831

Basically you can get items by tag name., like described in this link: http://www.vbaexpress.com/forum/showthread.php?t=31831

Dim HTMLdoc As HTMLDocument
Dim TDelements As IHTMLElementCollection   

Set HTMLdoc = ieApp.Document
Set TDelements = HTMLdoc.getElementsByTagName("table")

如果需要,您可以通过以下项目枚举:

And if you want, you can enumerate through the items:

Dim TDelement As HTMLTableCell
For Each TDelement In TDelements
    'code-code-code-code-code
Next

这篇关于把一张桌子从一堆HTML中抽出来高强的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆