使用 VBA 和 Excel 从一堆 HTML 中拉出一张表格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17535180/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 22:06:01  来源:igfitidea点击:

Pulling a table out of a mess of HTML w/ VBA & Excel

excel-vbahtml-parsingvbaexcel

提问by Bryan Heckler

'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

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

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

<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>

All I want out of this is the table that is <table class="table table-stripe">

我想要的只是这张桌子 <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.

但是由于我缺乏知识,我不确定从哪里开始修剪多余的部分,或者我是否应该尝试另一种方法。

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.

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

回答by Daniel M?ller

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

基本上你可以通过标签名称获取项目,就像在这个链接中描述的那样: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