excel清洗html标签,excel – 从HTML标记中的文件中删除文本

如果您使用的是Excel VBA,请将参考(工具 – 参考)设置为MSHTML库(参考菜单中标题为Microsoft HTML Object Library)

Sub ScrapeDateAbbr()

Dim hDoc As MSHTML.HTMLDocument

Dim hElem As MSHTML.HTMLGenericElement

Dim sFile As String, lFile As Long

Dim sHtml As String

'read in the file

lFile = FreeFile

sFile = "C:/Users/dick/Documents/My Dropbox/Excel/Testabbr.html"

Open sFile For Input As lFile

sHtml = Input$(LOF(lFile), lFile)

'put into an htmldocument object

Set hDoc = New MSHTML.HTMLDocument

hDoc.body.innerHTML = sHtml

'loop through abbr tags

For Each hElem In hDoc.getElementsByTagName("abbr")

'only those that have a data-utime attribute

If Len(hElem.getAttribute("data-utime")) > 0 Then

'get the title attribute

Debug.Print hElem.getAttribute("title")

End If

Next hElem

End Sub

我假设您在源文件中调用该文件是本地的.如果您需要先下载它,则需要另一个对MSXML和此代码的引用

Sub ScrapeDateAbbrDownload()

Dim xHttp As MSXML2.XMLHTTP

Dim hDoc As MSHTML.HTMLDocument

Dim hElem As MSHTML.HTMLGenericElement

Set xHttp = New MSXML2.XMLHTTP

xHttp.Open "GET", "file:///C:/Users/dick/Documents/My%20Dropbox/Excel/Testabbr.html"

xHttp.send

Do

DoEvents

Loop Until xHttp.readyState = 4

'put into an htmldocument object

Set hDoc = New MSHTML.HTMLDocument

hDoc.body.innerHTML = xHttp.responseText

'loop through abbr tags

For Each hElem In hDoc.getElementsByTagName("abbr")

'only those that have a data-utime attribute

If Len(hElem.getAttribute("data-utime")) > 0 Then

'get the title attribute

Debug.Print hElem.getAttribute("title")

End If

Next hElem

End Sub

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值