使用 VBA 循环遍历多个 div
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16679140/
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
Loop through multiple divs using VBA
提问by Ramesh
I am trying to extract information from a HTML page using Vb script. This is the HTML page from which I am trying to extract the information.
我正在尝试使用 Vb 脚本从 HTML 页面中提取信息。这是我试图从中提取信息的 HTML 页面。
<div id="profile-education">
<div class="position first education vevent vcard" id="xxxxxx">
University 1
<span class="degree">Ph.D.</span>
<span class="major">Computer Science</span>
<p class="period">
<abbr class="dtstart" title="2005-01-01">2005</abbr> – <abbr class="dtend"
title="2012-12-31">2012</abbr>
</div>
<div class="position education vevent vcard" id="xxxxxx">
University 2
<span class="degree">M.Eng.</span>
<span class="major">Computer Science</span>
<p class="period">
<abbr class="dtstart" title="2000-01-01">2000</abbr> – <abbr class="dtend"
title="2004-12-31">2004</abbr>
</p>
</div>
</div>
I want to extract the information in the below format.
我想以以下格式提取信息。
- University Name: University 1
- Degree Name: Phd
- Major: Computer Science
Period: 2005 - 2012
University Name: University 2
- Degree Name: M.Eng
- Major: Computer Science
- Period: 2000 - 2004
- 大学名称:大学1
- 学位名称:Phd
- 专业:计算机科学
时间:2005 - 2012
大学名称:大学2
- 学位名称:M.Eng
- 专业:计算机科学
- 时期:2000 - 2004
In my VB script, I have the following code which extracts the entire information as a single variable.
在我的 VB 脚本中,我有以下代码将整个信息提取为单个变量。
Dim openedpage as String
openedpage = iedoc1.getElementById("profile-education").innerText
However, if I use the following statement in my vb Script, I can get a particular span information.
但是,如果我在我的 vb 脚本中使用以下语句,我可以获得特定的跨度信息。
openedpage = iedoc1.getElementById("profile-education").getElementsByTagName("span")
(0).innerText
The above code gives me Phd as the output. However, I will not know the total spans beforehand and so I cannot simply give span(0) and span(1) in my code. Also, I would like to extract the information for all div tags and I won't be knowing this information either. Basically, I want some loop structure to iterate through the div tags with the id profile-educationfrom which I should be able to extract multiple div and span information.
上面的代码给了我 Phd 作为输出。但是,我事先不会知道总跨度,因此我不能简单地在我的代码中给出 span(0) 和 span(1)。另外,我想提取所有 div 标签的信息,我也不知道这些信息。基本上,我想要一些循环结构来遍历带有 id profile-education的 div 标签,我应该能够从中提取多个 div 和 span 信息。
回答by Tim Williams
Dim divs, div
set divs = iedoc1.getElementById("profile-education").getElementsByTagName("div")
for each div in divs
debug.print "*************************************"
debug.Print div.ChildNodes(0).toString
debug.print div.getElementsByTagName("span")(0).innerText
debug.print div.getElementsByTagName("span")(1).innerText
' etc...
next div