Web Scraping过滤结果

时间:2018-03-15 14:34:54

标签: excel vba excel-vba

所以我是vba的新手,我的价格是store.steampowered.com。我的价格很好,但我想过滤结果。如果结果低于2,则不要写结果。 我正在使用此代码:

y = 1

For x = 1 To 21

    objIE.navigate "http://example.com/market/search?appid=578080#p" & x & "x_name_asc"

    Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop

    MsgBox "Checking"

    For Each itemEle In objIE.document.getElementsByClassName("market_listing_row_link")

        Prc1 = itemEle.getElementsByClassName("market_listing_right_cell market_listing_their_price")(0).getElementsByTagName("span")(1).textContent

        Sheets("Sheet2").Range("D" & y).Value = Prc1

        y = y + 1

    Next

Next x

我尝试了这段代码并且无法正常工作:

If Prc1 > 2 Then

    Sheets("Sheet2").Range("D" & y).Value = Prc1

    y = y + 1

End If

例如在excel中:

D1 : $20.55 USD
D2 : $6.37 USD
D3 : $1.26 USD '-------> i am tring to dont get this

1 个答案:

答案 0 :(得分:1)

尝试以下方法。临时作为返回的类型之一不是字符串。

CDbl(Split(Split(Prc1, "$")(1), " ")(0)) > 2

确保从返回值中提取了数字部分,以便与值2进行比较。

Option Explicit

Sub testing()

    Dim x As Long, y As Long, Prc1 As String
    Dim objIE As Object
    Dim itemEle As IHTMLElement

    Set objIE = CreateObject("internetexplorer.application")

    y = 1

    For x = 1 To 21

        objIE.navigate "http://steamcommunity.com/market/search?appid=578080#p" & x & "x_name_asc"

        Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop

        For Each itemEle In objIE.document.getElementsByClassName("market_listing_row_link")

            On Error Resume Next

            Prc1 = itemEle.getElementsByClassName("market_listing_right_cell market_listing_their_price")(0).getElementsByTagName("span")(1).textContent

            If CDbl(Split(Split(Prc1, "$")(1), " ")(0)) > 2 Then

                Sheets("Sheet2").Range("D" & y).Value = Prc1
                y = y + 1
            End If

            On Error GoTo 0

        Next

    Next x

    objIE.Quit

End Sub