在目录内的HTML文件夹中按标签名称搜索元素 [英] Search elements by tag name in an HTML folder which is inside your directory

查看:402
本文介绍了在目录内的HTML文件夹中按标签名称搜索元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如你所看到的,这是一个加载HTML文档的代码片段,然后通过使用标签名称搜索它们来获取元素。

 'STATEMENT_1 
LoadPage IE,https://www.sec.gov/cgi-bin/browse-edgar? &安培; _
action = getcompany& CIK =& Ticker& & type =& InfoType& _
& dateb =& owner = exclude& count = 20
$ b'STATEMENT_2
Set els = IE.Document.getelementsbytagname(a)

然而,当 时,需要使用 Internet Explorer 来获取 HTML 文档,并且您的目录中有 HTML > ?



例如,如果我在目录 C:\中有 HTML 文档vsti 我如何指向 VBA 从这个位置进行取/取?

解决方案

这是一种使用IE与本地文件进行交互的方式。 (保存到桌面)

 < HTML> 
< HEAD>
<标题>我的示例< / Title>
< / HEAD>
< BODY>
< div class =find-me>
您找到了我
< / div>
< / BODY>
< / HTML>






Excel VBA (显示三个方法)

  Sub GetData()
Dim IE As InternetExplorer

'创建InternetExplorer对象
设置IE =新的InternetExplorerMedium

'您可以接受下一行查看页面加载
IE.Visible = False

'将URL设置为本地文件
IE.Navigate2C:\Users\PortlandRunner\Desktop\index.html

'等待IE浏览器加载.. 。可能不需要,因为它是本地
Do而IE.Busy
Application.Wait DateAdd(s,1,Now)
Loop

'选项A
Dim foundA As String
foundA = IE.Document.getElementsByClassName(find-me)(0).innerText
MsgBox foundA

'选项B
Dim foundB As Variant
Set foundB = IE.Document.getElementsByClassName(find-me)
MsgBox foundB(0).textConte nt

'Option C
Dim tag
Dim tags As Object
Set tags = IE.Document.getElementsByTagName(*)

对于每个标签In标签
如果tag.className =find-me则
MsgBox tag.innerText
退出对于
结束如果
下一个标签

'显示IE
'IE.Visible = True

'清理
设置IE = Nothing
End Sub






结果:



测试IE10& Excel 2010


As you can see, this is a code excerpt that loads an HTML document and then simply gets the element by searching them with the use of the tag name.

'STATEMENT_1
LoadPage IE, "https://www.sec.gov/cgi-bin/browse-edgar?" & _
                      "action=getcompany&CIK=" & Ticker & "&type=" & InfoType & _
                      "&dateb=&owner=exclude&count=20"

        'STATEMENT_2
        Set els = IE.Document.getelementsbytagname("a")

However what about when you do not need to make use of the Internet Explorer to fetch an HTML document and you have the HTML inside your directory?

For example if i have the HTML document in the directory C:\vsti how can i point VBA to go/fetch from this location?

解决方案

Here is one way to use IE to interact with a local file.

HTML File (Saved to desktop)

<HTML>
<HEAD>
  <Title>My Example</Title>
</HEAD>
<BODY>
  <div class="find-me">
    You Found Me
  </div>
</BODY>
</HTML>


Excel VBA (with three methods shown)

Sub GetData()
    Dim IE As InternetExplorer

    'Create InternetExplorer Object
    Set IE = New InternetExplorerMedium

    ' You can coment Next line To see page load
    IE.Visible = False

    'Set URL to local file
    IE.Navigate2 "C:\Users\PortlandRunner\Desktop\index.html"

    ' Wait while IE loading...  Probably don't need this since it's local
    Do While IE.Busy
        Application.Wait DateAdd("s", 1, Now)
    Loop

    'Option A
    Dim foundA As String
    foundA = IE.Document.getElementsByClassName("find-me")(0).innerText
    MsgBox foundA

    'Option B
    Dim foundB As Variant
    Set foundB = IE.Document.getElementsByClassName("find-me")
    MsgBox foundB(0).textContent

    'Option C
    Dim tag
    Dim tags As Object
    Set tags = IE.Document.getElementsByTagName("*")

    For Each tag In tags
        If tag.className = "find-me" Then
            MsgBox tag.innerText
            Exit For
        End If
    Next tag

    'Show IE
    'IE.Visible = True

    ' Clean up
    Set IE = Nothing
End Sub


Results:

Tested on IE10 & Excel 2010

这篇关于在目录内的HTML文件夹中按标签名称搜索元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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