Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用excelvba解析HTML数据_Html_Excel_Vba_Web Scraping_Screen Scraping - Fatal编程技术网

如何使用excelvba解析HTML数据

如何使用excelvba解析HTML数据,html,excel,vba,web-scraping,screen-scraping,Html,Excel,Vba,Web Scraping,Screen Scraping,我不熟悉使用excel VBA解析HTML数据。下面是我的代码和一些示例HTML。HTML中的注意事项:pt-DefaultParagraphFont-000016>职责和责任 我想打开数百个类似的内部网页,找到角色和职责部分,然后开始获取某些数据并将其粘贴到各个列中 我已经修改了下面的代码,它现在可以根据推荐的代码工作了 顺便说一句,为了防止这对其他人有帮助,我发现了一个很好的修复“object invoked has disconnected”错误的方法;下面是修复方法:Set ie=new

我不熟悉使用excel VBA解析HTML数据。下面是我的代码和一些示例HTML。HTML中的注意事项:pt-DefaultParagraphFont-000016>职责和责任

我想打开数百个类似的内部网页,找到角色和职责部分,然后开始获取某些数据并将其粘贴到各个列中

我已经修改了下面的代码,它现在可以根据推荐的代码工作了

顺便说一句,为了防止这对其他人有帮助,我发现了一个很好的修复“object invoked has disconnected”错误的方法;下面是修复方法:Set ie=newinternetexplorermedium

Option Explicit
Enum READYSTATE
READYSTATE_UNINITIALIZED = 0
READYSTATE_LOADING = 1
READYSTATE_LOADED = 2
READYSTATE_INTERACTIVE = 3
READYSTATE_COMPLETE = 4
End Enum

Sub ImportStackOverflowData()
    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>

角色和责任

关键数据治理角色

数据治理是一项共同的责任。您需要

html.getElementsByClassName("pt-000015")

它是复数形式,返回一个集合,并在末尾需要名称。

pt-000015是一个类元素,而不是id?您可以试试。getElementsByClassName-而不是getElementById。它不是XML,而是HTML。问题的标题是误导性的。谢谢你们,这就是我所需要的,只是一些让我越过这个障碍的信息。我真的很感谢你的帮助。