vba Excel getElementById 提取span类信息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16618799/
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
Excel getElementById extract the span class information
提问by Ramesh
I need to extract certain information from HTML using VBA.
我需要使用 VBA 从 HTML 中提取某些信息。
This is the HTML from which I am trying to extract the location information alone.
这是我试图单独提取位置信息的 HTML。
<dl id="headline" class="demographic-info adr">
<dt>Location</dt>
<dd>
<span class="locality">
Dallas/Fort Worth Area
</span>
</dd>
<dt>Industry</dt>
<dd class="industry">
Higher Education
</dd>
In my excel VBA, after opening the web page, I am using the following code to extract the information.
在我的excel VBA中,打开网页后,我使用以下代码提取信息。
Dim openedpage as String
openedpage = iedoc1.getElementById("headline").innerText
However, I am getting the information as,
但是,我得到的信息是,
Location Dallas/Fort Worth Area Industry Higher Education
位置达拉斯/沃斯堡地区工业高等教育
I just need to extract,
我只需要提取,
Dallas/Fort Worth Areaas the output.
达拉斯/沃斯堡地区作为输出。
回答by NickSlash
Try: iedoc1.getElementById("headline").getElementsByTagName("span")(0).innerText
尝试: iedoc1.getElementById("headline").getElementsByTagName("span")(0).innerText
Your getting all the extra text because that is kinda what you asked for, the innerText of the parent element, which is everything inside of it.
您获得了所有额外的文本,因为这正是您所要求的,即父元素的innerText,它是其中的所有内容。
The above code gets the content of the "headline" element, then finds all "span" tags inside of it. Looking at the list returned, it chooses the first instance and returns the innerText.
上面的代码获取“headline”元素的内容,然后找到其中的所有“span”标签。查看返回的列表,它选择第一个实例并返回innerText。
UpdateI always seem to get the index base wrong, the 1
in my example should have been a 0
更新我似乎总是把索引基弄错了,1
在我的例子中应该是一个0