VBA从跨度类中提取值

时间:2019-01-03 20:11:06

标签: html excel vba excel-vba web-scraping

我正在使用vba使用.getElementsByClassName从HTML提取值,但目前运气不佳。

我正在尝试从以下HTML中提取“ 20+”

<p class="delivery-stock">
    <span class="delivery-stock-value">20+</span> 
    <span class="delivery-available f-bold f4">available for delivery:</span>
</p>
<p>

到目前为止,我使用的VBA是

sh01.Cells(r, 5) = HTML.getElementsByClassName("delivery-stock-value")(0).innertext
sh01.Cells(r, 5) = HTML.getElementsByClassName("delivery-stock-value")(0).value

我也使用delivery-stock类来做同样的事情。

delivery-stock-valuedelivery-stock在HTML中仅出现一次。

预先感谢您的帮助。

2 个答案:

答案 0 :(得分:3)

这是一个小示例,应将文本返回到Sheet1中的单元格A1。您需要outerText属性。这只是出于演示目的,如果您希望它更快,更易维护,请使用Web请求。

Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Sub GetTheText()
    Dim ws As Worksheet: Set ws = ThisWorkbook.Worksheets("Sheet1")
    Dim text As String

    With CreateObject("internetexplorer.application")
        .Navigate "https://www.toolstation.com/2-column-radiator/p39711"
         Do While .Busy And .readyState <> 4:DoEvents:Loop
         Sleep 1000 ' wait a little bit too
         text = .document.getElementsByClassName("delivery-stock-value")(0).outerText
        .Quit
    End With

    ws.Cells(1, 1).Value = text
End Sub

答案 1 :(得分:1)

使用API​​(如果可用)。在这种情况下,您可以将股票代码传递给API。这更加有效和可靠。

使用JSONParser(推荐):

如果您想要JSON解析器(例如jsonconverter.bas)的安全性,请按照以下步骤操作。将.bas添加到项目后,请转到vbe>工具>引用,然后添加对Microsoft Scripting Runtime

的引用
Public Sub GetStock()
    Dim json As Object
    With CreateObject("MSXML2.XMLHTTP")
        .Open "GET", "https://www.toolstation.com/api/products/39711/delivery?site=WW", False
        .send
        Set json = JsonConverter.ParseJson(.responseText)
    End With
    Debug.Print json("data")("channels")("delivery")("stock")
End Sub

没有JSON解析器(不推荐):

Public Sub GetStock()
    Dim sResponse As String
    With CreateObject("MSXML2.XMLHTTP")
        .Open "GET", "https://www.toolstation.com/api/products/39711/delivery?site=WW", False
        .send
        sResponse = .responseText
    End With
    Debug.Print Split(Split(sResponse, "stock" & Chr$(34) & ":")(1), ",")(0)
End Sub