循环遍历从中提取多个数据的网页

时间:2014-06-08 12:09:55

标签: html excel vba excel-vba

1个月前,我问了一个关于如何从主页中提取一个值的问题,请参阅 Trying to extract ONE value from a webpage with VBA in Excel

我设法让它工作,但后来我意识到那里有股票的aloooooot,所以经过一番思考,我认为尝试制作某种列表页面的循环会更好。我找到了我正在寻找的页面 https://www.avanza.se/aktier/lista.html?countryCode=SE&marketPlaceOrList=MARKET_XSAT&sortField=NAME&sortOrder=ASCENDING&activeTab=quote

所以在查看了主页的源代码之后,我发现所有的span类都有相同的名称(在这里发布前两个)

<span class="se-14"></span>

<a href="/aktier/om-aktien.html/202888/203-web-group" class="link" title="203 Web Group">
        203 Web Group
    </a>

                        </td>
                        <td class="quote tRight noSort changePercent unknown">2.11</td>
                        <td class="quote tRight noSort change unknown">1.30</td>
                        <td class="quote tRight noSort buyPrice">120.00</td>
                        <td class="quote tRight noSort sellPrice">118.00</td>
                        <td class="quote tRight noSort lastPrice"><span class="pushBox roundCorners3">119.50</span></td>
                        <td class="quote tRight noSort highestPrice">122.00</td>
                        <td class="quote tRight noSort lowestPrice">117.00</td>
                        <td class="quote tRight noSort totalVolumeTraded">20000</td>
                        <td class="quote tRight noSort updated">-</td>
                        <td class="quote tRight noSort tools clearFix">

    <ul class="orderbook_tools cleanList floatList fLeft">

            <li>
                <a title="Lägg till kurslarm" class="icon alertIco triggerLogin" href="javascript:void(0)"></a>
            </li>

            <li>
                <a title="Visa anteckningar" class="icon noteIco triggerLogin" href="javascript:void(0)"></a>
            </li>

            <li>
                <a title="Lägg till i bevakningslistor" class="icon watchListIco triggerLogin" href="javascript:void(0)"></a>
            </li>

    </ul>

                            <input type="checkbox" name="compareOrderbooks" value="202888"  />
                        </td>
                    </tr>

                    <tr class="rowHighlight" data-oid="405356" data-delayed="true">
                        <td class="quote tLeft buySellButtons noSort">
    <ul class="cleanList floatList actionButtons buySellButtons">
        <li class="first">

                    <a href="/handla/aktier.html/kop/405356/a1m-pharma" title="Köp" class="orderLink XXSText">K</a>

        </li>
        <li class="last">

                    <a href="/handla/aktier.html/salj/405356/a1m-pharma" title="Sälj" class="orderLink XXSText">S</a>

        </li>
    </ul>
</td>
                        <td class="quote tLeft instrumentName">

<span class="se-14"></span>

    <a href="/aktier/om-aktien.html/405356/a1m-pharma" class="link" title="A1M Pharma">
        A1M Pharma
    </a>

                        </td>
                        <td class="quote tRight noSort changePercent unknown">1.22</td>
                        <td class="quote tRight noSort change unknown">1.11</td>
                        <td class="quote tRight noSort buyPrice">50.00</td>
                        <td class="quote tRight noSort sellPrice">48.00</td>
                        <td class="quote tRight noSort lastPrice"><span class="pushBox roundCorners3">49.50</span></td>
                        <td class="quote tRight noSort highestPrice">50.50</td>
                        <td class="quote tRight noSort lowestPrice">45.00</td>
                        <td class="quote tRight noSort totalVolumeTraded">30000</td>
                        <td class="quote tRight noSort updated">-</td>
                        <td class="quote tRight noSort tools clearFix">

    <ul class="orderbook_tools cleanList floatList fLeft">

可以看出,span类具有相同的名称(se-14)。

那么,我想做什么!

  1. 首先,在标题下提取信息(第一个案例203 Web Group并放入单元格An(例如A2))。
  2. 第二,提取股票的最后价格(名为LastPrice,例如第一股的119.50)并将其放入B2单元格
  3. 转到下一个股票,把它放在A(n + 1)中,例如将下一个公司的名称,A1m pharma放在单元格A3中,将其最新价格放在单元格B3中等等。
  4. 所以问题是,当所有具有相同的span类名时,是否可以遍历页面? 并提取整个列表?并且放入我想要的maner,并且cellname可以是一个变量,比如An,对于vba-script运行的每个循环,n = n + 1?

    由于我对Excel很陌生,请用非常基本的语言回复=) 最诚挚的问候

1 个答案:

答案 0 :(得分:0)

也许有人可以为你写一个正则表达式。不是我。
这只是一个编程问题:

Option Explicit

Sub Sub1()
' assumes the html is in column C
' assumes roundCorners3 is a good pointer to lastprice
  Dim iposL&, iposR&, iLastRowC&, irowAB&, sCellC$, sTitle$, sLastPrice$
  irowAB = 2
  Do
    sCellC = getCellC()
    If sCellC = "EOD" Then Exit Do
    If InStr(1, sCellC, "<span class=""se-14"">") > 0 Then
      Do
        sCellC = getCellC()
        iposL = InStr(1, sCellC, "title=")
      Loop Until iposL > 0
      iposR = InStr(iposL, sCellC, ">")
      sTitle = Mid$(sCellC, iposL + 7, iposR - iposL - 7 - 1)
      Do
        sCellC = getCellC()
        iposL = InStr(1, sCellC, "roundCorners3")
      Loop Until iposL > 0
      sLastPrice = Val(Mid$(sCellC, iposL + 15))
      Cells(irowAB, 1) = sTitle
      Cells(irowAB, 2) = sLastPrice
      irowAB = irowAB + 1
    End If
  DoEvents
  Loop
End Sub

Function getCellC$()
  Static RowC&, RowCLast&
  If RowC = 0 Then RowCLast = Cells(Rows.Count, "C").End(xlUp).Row
  RowC = RowC + 1
  If RowC <= RowCLast Then
    getCellC = Cells(RowC, 3)
  Else
    getCellC = "EOD"
  End If
End Function