Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
300 views
in Technique[技术] by (71.8m points)

excel - How to get web elements using VBA

I basically want to be able to read the players rank using VBA in Excel but haven't been able to get it working. I get a value error and can't figure out why. I only have a very basic knowledge of VBA so please be patient.

So I've changed the code to whats shown down below but when run in excel it returns a value of 0. Nevermind worked when I restarted excel. Thanks for all the help!

    Function player_battlerank(player_name As String)
start1:
Set objhttp1 = New WinHttp.WinHttpRequest
Dim doc1 As MSHTML.HTMLDocument
objhttp1.Open "GET", "http://ps2.fisu.pw/player/?name=" & player_name, True
objhttp1.send

objhttp1.waitforresponse

If objhttp1.status <> 200 Then GoTo start1
Set doc1 = New MSHTML.HTMLDocument
doc1.body.innerHTML = objhttp1.responseText

Set obj1 = doc1.getelementbyid("stat_overview")
For Each obj3 In obj1.getelementsbytagname("div")
    If obj3.innerText Like "*BATTLE RANK*" Then
        player_battlerank = obj3.getelementsbytagname("b")(0).innerText
        Exit Function
    End If
Next obj3
End Function
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Something like this should work:

'....
Set doc1 = New MSHTML.HTMLDocument
doc1.body.innerHTML = objhttp1.responseText

Set obj1 = doc1.getelementbyid("stat_overview")
For Each obj3 In obj1.getelementsbytagname("div")
    If obj3.innerText Like "*BATTLE RANK*" Then
        player_rank = obj3.getelementsbytagname("b")(0).innerText
        Exit Function
    End If
Next obj3
'....

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...