【发布时间】:2019-04-20 16:05:29
【问题描述】:
我正在尝试使用 MSXML2 和 IHTMLDocument 处理 HTML 网页的 iframe 部分。
我想使用 MSXML2 并将其保存以更好地“捕获”数据,认为它比仅使用 InternetExplorer 或 VBA 菜单支持的 VBA selenium 参考更快。 (我不想尽可能避免使用 IE 或 selenium)
但我不知道如何将文档保存为 XML 格式(以利用其速度),同时在没有浏览器(即 selenium)帮助的情况下单击文档中的元素。
即使在this web page 上单击某些选项卡(id="cns_Tab21")后,我也难以检索数据。
所以我的问题是..
1> 是否可以尽量减少使用浏览器进行点击?
2> 即使在单击(使用 Selenium)之后,它也会在 VBA 编辑器中引发与 xpath 相关的错误。
感谢您提前回答,用于此的 URL 是 http://bitly.kr/finance 并且链接内的 iframe 是http://bitly.kr/LT0aCb
'I declared objects
Dim XMLReq As New MSXML2.XMLHTTP60
Dim HTMLDoc As New MSHTML.HTMLDocument
Dim iframeDoc As IHTMLDocument
'and saved XML data to HTML format
HTMLDoc.body.innerHTML = XMLReq.responseText
'and trying to save this HTML to iframe...
Set iframeDoc = HTMLDoc.getElementById("coinfo_cp")
'I tried .contentDocument but it maybe HTMLdoc doesn't have this property.
and I don't know how to access information I saved to iframeDoc above.
'And after I use Selenium I can't figure out why it throw an error
For Each ele In selenium.FindElementsByTag("th")
If ele.Attribute("innerText") = "CAPEX" Then
Debug.Print ele.FindElementsByXPath("./../td").Attribute("innerText")
这篇文章不是重复的,因为我正在尝试使用 XML 来处理 iframe 元素,并且在 VBA Excel 中没有 InternetExplorer 引用。(ie.document)
【问题讨论】:
-
请问网址是什么?
-
@QHarr 谢谢。 bitly.kr/finance不是英文页面但是中间有一个iframe标签
-
iframe 有不少。你要哪个?你需要什么数据?
-
@QHarr 对不起,我给了你错误的 URL;页面 URL 为 bitly.kr/RWz5x,iframe id 为“coinfo_cp”
标签: excel vba web-scraping