简体   繁体   English

VBA中的Excel Web查询

[英]excel web query in vba

I have a worksheet that currently updates about 200 stocks using Yahoo Finance API and MSXML. 我有一个工作表,当前使用Yahoo Finance API和MSXML更新约200只股票。 I would like to also get some other info from other websites that don't have an api, for example " http://www.earningswhisper.com/stocks.asp?symbol=googl ". 我还想从没有api的其他网站上获取其他信息,例如“ http://www.earningswhisper.com/stocks.asp?symbol=googl ”。

Also for example, if you look at some info from that web page below, you see there is a Release Date of 1/29/2015. 同样,例如,如果您从下面的网页上查看某些信息,则会看到发布日期为2015年1月29日。 There is also some text in between, currently "[not confirmed]", but at some point it will change to "[confirmed]" and both items of text are of interest. 两者之间也有一些文本,当前为“ [未确认]”,但有时会更改为“ [已确认]”,并且这两个文本项都是您感兴趣的。

For lack of better web skills, I currently have a single sheet with a QueryTable that is refreshed in a code synchronously. 由于缺乏更好的Web技能,我目前只有一张带有QueryTable的工作表,该表在代码中同步刷新。 It works...eventually. 最终会起作用。 I would rather work with the response in code, but I don't know how to do that. 我宁愿使用代码中的响应,但我不知道该怎么做。 I don't need this particular info to be auto refreshed. 我不需要此特定信息即可自动刷新。

Questions 问题

  1. Is there a preferred VBA way work with the html response? 是否有首选的VBA方式处理html响应? Can you show a code snippet to illustrate? 您可以显示一个代码片段进行说明吗?
  2. Is it possible to convert the html to xml or json relatively easy? 是否有可能相对容易地将html转换为xml或json? code snippet? 代码段?
  3. If QueryTable is in fact the good enough solution, would it be faster to create a sheet for each stock and refresh async, using events? 如果QueryTable实际上是足够好的解决方案,那么使用事件为每个股票创建一张表格并刷新异步会更快吗?

I know there is oodles of info on the web, but most of it seems dated and confusing. 我知道网络上有很多信息,但其中大多数似乎过时且令人困惑。 I am using Excel 2013. 我正在使用Excel 2013。

在此处输入图片说明

I am able to get at the data I want using html and grabbing Table(6) as shown in the code below. 我可以使用html并获取Table(6)来获取所需的数据,如下面的代码所示。 I guess I could parse the InnerText but I suspect there is an easier way to grab the elements I need from that table. 我想我可以解析InnerText,但是我怀疑有一种更简单的方法可以从该表中获取所需的元素。

Sub TestHtml()
   Dim Resp, sText, FirstCode As String
   Dim oHttp, oFile, oTable As Object
   Dim lines As Variant

   Set oHttp = CreateObject("Microsoft.XMLHttp")
   oHttp.Open "GET", "http://www.earningswhisper.com/stocks.asp?   symbol=googl", False
   oHttp.send ("")
   Resp = oHttp.responseText
   Set oFile = CreateObject("htmlfile")
   oFile.Write Resp
   Set oTable = oFile.getElementsByTagName("Table")(6)
   sText = oTable.innertext
   MsgBox sText
End Sub

Here is a line from that table, and the full table below. 这是该表的一行,下面是完整的表。

 <tr onMouseover="this.className='newsart_s2'" onMouseOut="this.className='newsart'"> <td width="67%" align=left valign=middle>&nbsp;Release Date: <font color='#505050'><small>[not confirmed]</small></font> </td> <td width="33%" align=right valign=middle><a href="calendar.asp?day=-5">1/29/2015</a> &nbsp;</td> </tr> 

What is the best way to drill down and get at the elements in the table, using VBA code? 使用VBA代码向下钻取并获取表中元素的最佳方法是什么?

 <TABLE cellpadding=1 cellspacing=0 border=0 id=QEsts width="100%" bgcolor="#505050"><tr><td><TABLE cellpadding=2 cellspacing=0 border=0 width="100%" bgcolor="#FFFFFF" height='148'><tr><td valign=top> <table cellpadding=0 cellspacing=0 border=0 width="100%" class='newsart'> <tr><td colspan="2" bgcolor="#FFFFFF"><table width="100%" bgcolor="#FFFFFF" cellpadding=1 cellspacing=0 border=0><tr><td style="background-image: url('images/headbar2.gif'); background-color: #000000; BORDER-RIGHT: #000000 thin solid; BORDER-TOP: #000000 thin solid; FONT-WEIGHT: bold; FONT-SIZE: 12px; MARGIN: 2px; BORDER-LEFT: #000000 thin solid; COLOR: #e1b64b; BORDER-BOTTOM: #000000 thin solid; FONT-FAMILY: Arial;">&nbsp; 4th Quarter Ending December 2014</td></tr></table></td></tr> <tr onMouseover="this.className='newsart_s2'" onMouseOut="this.className='newsart'"><td width="67%" align=left valign=middle> &nbsp;<b>Earnings Whisper</b> <small>&#174</small>: </td><td width="33%" align=right valign=middle><b>$7.24</b> &nbsp;</td></tr> <tr onMouseover="this.className='newsart_s2'" onMouseOut="this.className='newsart'"><td width="67%" align=left valign=middle> &nbsp;Consensus Estimate:</td><td width="33%" align=right valign=middle>$7.16 &nbsp;</td></tr> <tr onMouseover="this.className='newsart_s2'" onMouseOut="this.className='newsart'"><td width="67%" align=left valign=middle> &nbsp;Surprise Expectation <small><sup>1</sup></small>: </td><td width="33%" align=right valign=middle> &nbsp;</td></tr> <tr onMouseover="this.className='newsart_s2'" onMouseOut="this.className='newsart'"><td width="67%" align=left valign=middle> &nbsp;Release Date: <font color='#505050'><small>[not confirmed]</small></font></td><td width="33%" align=right valign=middle><a href="calendar.asp?day=-5">1/29/2015</a> &nbsp;</td></tr> <tr><td width="100%" align=right colspan="2" valign=middle>After Close &nbsp;</td></tr> <tr onMouseover="this.className='newsart_s2'" onMouseOut="this.className='newsart'"><td width="67%" align=left valign=middle> &nbsp;Expected Time <small><sup>2</sup></small>: </td><td width="33%" align=right valign=middle>N/A &nbsp;</td></tr> <tr onMouseover="this.className='newsart_s2'" onMouseOut="this.className='newsart'"><td width="67%" align=left valign=top> &nbsp;Conference Call: </td><td width="33%" align=right valign=top>4:30 PM ET &nbsp;<small><br> &nbsp;</small></td></tr> </table> </td></tr></TABLE></td></tr></TABLE> </td> 

You can interact with DOM since you created that object successfully, but you know in your case the easiest way to get the necessary value from the table above is parsing the InnerText as string, try the code below: 自成功创建对象以来,您就可以与DOM进行交互,但是您知道从上表中获取必要值的最简单方法是将InnerText解析为字符串,请尝试以下代码:

aTmp0 = Split(sText, "Release Date:")
If Ubound(aTmp0) = 1 Then
    aTmp1 = Split(aTmp0(1), "Expected Time")
    MsgBox aTmp1(0)
Else
    MsgBox "Release Date not found"
End If

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM