【发布时间】:2014-03-30 20:09:17
【问题描述】:
在下面的代码中,我们使用 IE 自动化从这里获取
位置 1
"https://www.sec.gov/cgi-bin/browse-edgar?action=getcompany&CIK=" & Ticker & "&type=10-Q&dateb=&owner=exclude&count=20"
到这样的位置
位置 2
https://www.sec.gov/Archives/edgar/data/10795/000119312514042815/bdx-20131231.xml
有没有办法从位置 1 到位置 2,而不使用 IE 自动化并找到更可靠、安全和更快的东西?
为了完整起见,这里是我们现在拥有的完整代码;通过运行,您会看到大量使用 IE:
Option Explicit
Sub MadMule2()
Dim IE As InternetExplorer
Dim el
Dim els
Dim colDocLinks As New Collection
Dim Ticker As String
Dim lnk
Dim intCounter as Integer
Set IE = New InternetExplorer
IE.Visible = False
Ticker = Worksheets("Sheet1").Range("A1").Value
LoadPage IE, "https://www.sec.gov/cgi-bin/browse-edgar?" & _
"action=getcompany&CIK=" & Ticker & "&type=10-Q" & _
"&dateb=&owner=exclude&count=20"
Set els = IE.document.getElementsByTagName("a")
For Each el In els
If Trim(el.innerText) = "Documents" Then
colDocLinks.Add el.href
End If
Next el
intCounter = 1
For Each lnk In colDocLinks
LoadPage IE, CStr(lnk)
For Each el In IE.document.getElementsByTagName("a")
If el.href Like "*[0-9].xml" Then
ActiveWorkbook.XmlMaps.Add(el, "xbrl").Name = "xbrl Map"
End If
Next el
Next lnk
End Sub
Sub LoadPage(IE As InternetExplorer, URL As String)
IE.navigate URL
Do While IE.Busy Or IE.readyState <> READYSTATE_COMPLETE
DoEvents
Loop
End Sub
附加
问:Is there a way to go from location 1 to location 2 without using IE Automation and finding something more reliable, secure and faster?
你能扩展一下吗?
按我的意思
A:这是对我们收到的 user2140261 here 的代码块的评论:
你应该看看 MSXML,它比 IE 自动化更快、更安全、更可靠。
由于代码打开 Internet Explorer,解析源页面以查找 href 并到达所需的 Web 位置;我们想知道是否有办法在不使用 IE 的情况下进入位置 2。可以像 user2140261 所说的那样使用 MSXML 来完成吗?
【问题讨论】:
-
Is there a way to go from location 1 to location 2 without using IE Automation and finding something more reliable, secure and faster?你能详细说明一下吗?你有什么具体的想法吗?going from location 1 to location 2是什么意思? -
我已经修改了我的帖子。如果解释充分,请通知我
-
我刚看到。我会仔细研究
-
url 是服务器上存储 xml 文件的位置。 Ajax 调用是对在后台运行的 url 的 HTTP 请求,它连接到服务器而不通过客户端(爬取 DOM),就像您在示例中所做的那样。在我发布的链接示例中,返回了 JSON 对象,这就是使用 json 解析器作为示例的原因。如果您使用 XML,您可能需要在 VBA 中寻找 XML 解析器。
标签: xml vba excel web-scraping