如何使用 Excel VBA 解析 HTML 数据
How to parse HTML data using Excel VBA
我不熟悉使用 excel VBA 解析 HTML 数据。下面是我的代码和一些示例 HTML。 HTML 中的通知:pt-DefaultParagraphFont-000016">角色和职责
我想打开数百个类似的内部网页,找到“角色和职责”部分,然后开始抓取某些数据并将其粘贴到各个栏目中。
我已经修改了下面的代码,它现在可以根据推荐的内容运行了。
顺便说一句,以防万一这对其他人有帮助,我确实发现了 'object invoked has disconnected' 错误的一个很好的修复方法;这是解决方法:设置 ie = New InternetExplorerMedium
Option Explicit
Enum READYSTATE
READYSTATE_UNINITIALIZED = 0
READYSTATE_LOADING = 1
READYSTATE_LOADED = 2
READYSTATE_INTERACTIVE = 3
READYSTATE_COMPLETE = 4
End Enum
Sub ImportWhosebugData()
Dim a As String
Dim i As Long
Dim ie As InternetExplorer
Dim html As HTMLDocument
'Set ie = New InternetExplorer 'replaced with InternetExplorerMedium to fix error
Set ie = New InternetExplorerMedium 'this fixes this error: The object invoked has disconnected from its client
ie.Visible = False
ie.navigate "policy.myurl.com"
Do While ie.READYSTATE <> READYSTATE_COMPLETE
DoEvents
Loop
Set html = ie.document
Dim tag As IHTMLElement
Dim tags As IHTMLElementCollection
Set tags = html.getElementsByClassName("pt-000015")
For Each tag In tags
'more logic here
Next
Set html = ie.document
Set ie = Nothing
End Sub
HTML 样本:
div class="pt-000015">
我不熟悉使用 excel VBA 解析 HTML 数据。下面是我的代码和一些示例 HTML。 HTML 中的通知:pt-DefaultParagraphFont-000016">角色和职责
我想打开数百个类似的内部网页,找到“角色和职责”部分,然后开始抓取某些数据并将其粘贴到各个栏目中。
我已经修改了下面的代码,它现在可以根据推荐的内容运行了。
顺便说一句,以防万一这对其他人有帮助,我确实发现了 'object invoked has disconnected' 错误的一个很好的修复方法;这是解决方法:设置 ie = New InternetExplorerMedium
Option Explicit
Enum READYSTATE
READYSTATE_UNINITIALIZED = 0
READYSTATE_LOADING = 1
READYSTATE_LOADED = 2
READYSTATE_INTERACTIVE = 3
READYSTATE_COMPLETE = 4
End Enum
Sub ImportWhosebugData()
Dim a As String
Dim i As Long
Dim ie As InternetExplorer
Dim html As HTMLDocument
'Set ie = New InternetExplorer 'replaced with InternetExplorerMedium to fix error
Set ie = New InternetExplorerMedium 'this fixes this error: The object invoked has disconnected from its client
ie.Visible = False
ie.navigate "policy.myurl.com"
Do While ie.READYSTATE <> READYSTATE_COMPLETE
DoEvents
Loop
Set html = ie.document
Dim tag As IHTMLElement
Dim tags As IHTMLElementCollection
Set tags = html.getElementsByClassName("pt-000015")
For Each tag In tags
'more logic here
Next
Set html = ie.document
Set ie = Nothing
End Sub
HTML 样本:
div class="pt-000015">