【发布时间】:2014-02-07 23:37:42
【问题描述】:
我想从网页http://www.eex.com/en/market-data/power/derivatives-market/phelix-futures获取一些数据。
如果我使用旧的 InternetExplorer 对象(下面的代码),我可以浏览 HTML 文档。但我想使用XMLHTTP 对象(第二个代码)。
Sub IEZagon()
'we define the essential variables
Dim ie As Object
Dim TDelement, TDelements
Dim AnhorLink, AnhorLinks
'add the "Microsoft Internet Controls" reference in your VBA Project indirectly
Set ie = CreateObject("InternetExplorer.Application")
With ie
.Visible = True
.navigate ("[URL]http://www.eex.com/en/market-data/power/derivatives-market/phelix-futures[/URL]")
While ie.ReadyState <> 4
DoEvents
Wend
Set AnhorLinks = .document.getElementsbytagname("a")
Set TDelements = .document.getElementsbytagname("td")
For Each AnhorLink In AnhorLinks
Debug.Print AnhorLink.innertext
Next
For Each TDelement In TDelements
Debug.Print TDelement.innertext
Next
End With
Set ie = Nothing
End Sub
使用带有 XMLHTTP 对象的代码:
Sub FuturesScrap(ByVal URL As String)
Dim XMLHttpRequest As XMLHTTP
Dim HTMLDoc As New HTMLDocument
Set XMLHttpRequest = New MSXML2.XMLHTTP
XMLHttpRequest.Open "GET", URL, False
XMLHttpRequest.send
While XMLHttpRequest.readyState <> 4
DoEvents
Wend
Debug.Print XMLHttpRequest.responseText
HTMLDoc.body.innerHTML = XMLHttpRequest.responseText
With HTMLDoc.body
Set AnchorLinks = .getElementsByTagName("a")
Set TDelements = .getElementsByTagName("td")
For Each AnchorLink In AnchorLinks
Debug.Print AnhorLink.innerText
Next
For Each TDelement In TDelements
Debug.Print TDelement.innerText
Next
End With
End Sub
我只得到基本的 HTML:
<html>
<head>
<title>Resource Not found</title>
<link rel= 'stylesheet' type='text/css' href='/blueprint/css/errorpage.css'/>
</head>
<body>
<table class="header">
<tr>
<td class="CMTitle CMHFill"><span class="large">Resource Not found</span></td>
</tr>
</table>
<div class="body">
<p style="font-weight:bold;">The requested resource does Not exist.</p>
</div>
<table class="footer">
<tr>
<td class="CMHFill"> </td>
</tr>
</table>
</body>
</html>
我想浏览表格和对应的数据... 最后我想从年到月选择不同的时间间隔:
非常感谢任何帮助!谢谢!
【问题讨论】:
-
看起来您请求的 URL 不正确...
-
我正在整理正确的网址:
-
见@brettdj 的回复HERE
-
我昨天读了这篇文章,但我没有找到关于使用 XMLHTTP 对象抓取页面的答案。 Www 我正在使用 javascript 来显示所有财务数据,所以问题在于 readystate 和使用 XMLHTTP 对象的权利。我没有使用旧的 InternetExplorer 的问题。但它很慢而且不舒服......
标签: html vba excel web-scraping