使用VBA Excel

时间:2017-08-08 15:10:46

标签: html vba

我正试图通过使用ie.doc.innertext和星号从网站中提取价格,但它不起作用。

例如,innertext中的价格为:“购物车总额:25美元”

我的条件是:如果innertext中存在“添加到购物车中的商品”字样,并且innertext中存在“Cart Total”,则在innertext中提取冒号后的部分Cart Total:$ 25

我想要 $ 25

我的代码是:

Sub GetPrice()

Dim IE As New InternetExplorer
Dim doc As HTMLDocument


IE.Visible = True
IE.Navigate " http://www.example.com"

Do Until IE.ReadyState = READYSTATE_COMPLETE
    DoEvents
Loop

Set doc = IE.Document
doc.forms("shipform").Elements("upcs").Value = "025192197925"

Application.Wait Now + TimeValue("00:00:01")


doc.forms("shipform").Elements("submit").Click

Do Until IE.ReadyState = READYSTATE_COMPLETE
    DoEvents
Loop

Application.Wait Now + TimeValue("00:00:01")

html_code = doc.body.innerText

If InStr(html_code, "Item added to the cart") > 0 Then

    If InStr(html_code, "Cart Total " & "*") > 0 Then
        the_string = Right("Cart Total " & "*", 15)
        MsgBox the_string
    End If

ElseIf InStr(html_code, "Item not added") > 0 Then
    MsgBox "Not added"
End If

Set IE = Nothing

End Sub

我非常感谢你的帮助。谢谢

2 个答案:

答案 0 :(得分:1)

这样的事情应该有效:

Dim pos as Long

If InStr(html_code, "Item added to the cart") > 0 Then

    pos = InStr(html_code, "Cart Total")

    If pos > 0 Then

        the_string = Mid(html_code,pos+10, 5)
        MsgBox the_string

    End If

Else Then
    MsgBox "Not added"
End If

答案 1 :(得分:0)

尝试这些给你一个想法

debug.print split("Cart Total: $25")(2)          ' space is the default split character

debug.print split("Cart Total: $25", ":")(1)