VBA如何从Web刮取表格

时间:2019-10-24 03:21:00

标签: html vba html-table

尝试对从网上抓取表格的VBA进行编码。由于For r = 0 To (elemcollection(t).Rows.Length - 1)上的错误438,它无法正常工作。谢谢您,您有一些专业知识吗?

Public Sub GetPrice()
Dim ie As Object, t As Integer, r As Integer, c As Integer, elemcollection As Object, URL As String, eRowa As Long, eRowb As Long, eRowc As Long

URL = "https://finance.yahoo.com/quote/AAPL/history?period1=1413993600&period2=1571760000&interval=1mo&filter=history&frequency=1mo"

Set ie = CreateObject("internetexplorer.application")

With ie
  .Visible = True
  .navigate URL
While .Busy Or .readyState < 4: DoEvents: Wend

Set elemcollection = ie.document.getElementsByClassName("W(100%) M(0)")
   For t = 0 To (elemcollection.Length - 1)
    For r = 0 To (elemcollection(t).Rows.Length - 1)
     eRowa = Sheet2.Cells(Rows.Count, 62).End(xlUp).Row
      For c = 0 To (elemcollection(t).Rows(r).Cells.Length - 1)

      ThisWorkbook.Worksheets(2).Cells(eRowa + 1, c + 61) = elemcollection(t).Rows(r).Cells(c).innerText

     Next c
   Next r
  Next t

 End With

end sub

例如html中的

<table class="W(100%) M(0)" data-test="historical-prices" data-reactid="31"><thead data-reactid="32"><tr class="C($tertiaryColor) Fz(xs) Ta(end)" data-reactid="33"><th class="Ta(start) W(100px) Fw(400) Py(6px)" data-reactid="34"><span data-reactid="35">Date</span></th><th class="Fw(400) Py(6px)" data-reactid="36"><span data-reactid="37">Open</span></th><th class="Fw(400) Py(6px)" data-reactid="38"><span data-reactid="39">High</span></th><th class="Fw(400) Py(6px)" data-reactid="40"><span data-reactid="41">Low</span></th><th class="Fw(400) Py(6px)" data-reactid="42"><span data-reactid="43">Close*</span></th><th class="Fw(400) Py(6px)" data-reactid="44"><span data-reactid="45">Adj Close**</span></th><th class="Fw(400) Py(6px)" data-reactid="46"><span data-reactid="47">Volume</span></th></tr></thead><tbody data-reactid="48"><tr class="BdT Bdc($seperatorColor) Ta(end) Fz(s) Whs(nw)" data-reactid="49"><td class="Py(10px) Ta(start) Pend(10px)" data-reactid="50"><span data-reactid="51">Oct 01, 2019</span></td><td class="Py(10px) Pstart(10px)" data-reactid="52"><span data-reactid="53">104.00</span></td><td class="Py(10px) Pstart(10px)" data-reactid="54"><span data-reactid="55">104.64</span></td><td class="Py(10px) Pstart(10px)" data-reactid="56"><span data-reactid="57">97.11</span></td><td class="Py(10px) Pstart(10px)" data-reactid="58"><span data-reactid="59">97.36</span></td><td class="Py(10px) Pstart(10px)" data-reactid="60"><span data-reactid="61">97.36</span></td><td class="Py(10px) Pstart(10px)" data-reactid="62"><span data-reactid="63">89,759,200</span></td></tr><tr class="BdT Bdc($seperatorColor) Ta(end) Fz(s) Whs(nw)" data-reactid="64"><td class="Py(10px) Ta(start) Pend(10px)" data-reactid="65"><span data-reactid="66">Sep 01, 2019</span></td><td class="Py(10px) Pstart(10px)" data-reactid="67"><span data-reactid="68">107.92</span></td><td class="Py(10px) Pstart(10px)" data-reactid="69"><span data-reactid="70">111.67</span></td><td class="Py(10px) Pstart(10px)" data-reactid="71"><span data-reactid="72">101.10</span></td><td class="Py(10px) Pstart(10px)" data-reactid="73"><span data-reactid="74">103.59</span></td><td class="Py(10px) Pstart(10px)" data-reactid="75"><span data-reactid="76">103.59</span></td><td class="Py(10px) Pstart(10px)" data-reactid="77"><span data-reactid="78">134,941,900</span></td></tr><tr class="BdT Bdc($seperatorColor) Ta(end) Fz(s) Whs(nw)" data-reactid="79"><td class="Py(10px) Ta(start) Pend(10px)" data-reactid="80"><span data-reactid="81">Aug 01, 2019</span></td><td class="Py(10px) Pstart(10px)" data-reactid="82"><span data-reactid="83">110.45</span></td><td class="Py(10px) Pstart(10px)" data-reactid="84"><span data-reactid="85">112.45</span></td><td class="Py(10px) Pstart(10px)" data-reactid="86"><span data-reactid="87">102.21</span></td><td class="Py(10px) Pstart(10px)" data-reactid="88"><span data-reactid="89">109.05</span></td><td class="Py(10px) Pstart(10px)" data-reactid="90"><span data-reactid="91">109.05</span></td>

0 个答案:

没有答案