'Macro to Fetch 'Blood&Soul' Game Character Profile
'by konahn(at)naver.com
'At [Tools-Reference], 'Microsoft Html Object Library' and 'Microsoft Scripting Engine' should be checked.
'Powered by VBA-JASON (c) Tim Hall - https://github.com/VBA-tools/VBA-JSON
Option Explicit
Option Compare Text
Sub getProfile()
Dim XMLhttp As Object 'MSXML2.ServerXMLHTTP
Dim Html As MSHTML.HTMLDocument
Dim elems As MSHTML.IHTMLElementCollection
Dim lastRow As Long
Dim r As Range
Dim Info() As String, Equip() As String
Dim JsonStr As String
Dim Json As Object, J As Object
Dim usr As VbMsgBoxResult
usr = MsgBox("과도한 검색은 사이트에 무리를 줄 수 있습니다." & vbNewLine & vbNewLine & _
"테스트목적으로만 사용하시겠습니까?", vbOKCancel, "Getting BnS Profile")
If usr <> vbOK Then Exit Sub
Set XMLhttp = CreateObject("MSXML2.ServerXMLHTTP")
'Set XMLhttp = New MSXML2.ServerXMLHTTP
Set Html = New MSHTML.HTMLDocument
If XMLhttp Is Nothing Or Html Is Nothing Then GoTo DONE
Application.ScreenUpdating = False
lastRow = Cells(Rows.Count, 1).End(xlUp).Row
Range("B5:Z" & lastRow).ClearContents
On Error Resume Next
For Each r In Range("A5:A" & lastRow)
'// 기본 정보
With XMLhttp
.Open "Get", Range("B1").Value & ENCODEURL(r.Value), False
.setRequestHeader "User-Agent", "Mozilla"
.send
.waitForResponse
Html.body.innerHTML = .responseText
End With
'암살자| 경국지색| 레벨 50(•) 홍문2성| 혼천교입교인| 활빈당
Set elems = Html.getElementsByClassName("info")
If elems.Length = 0 Then MsgBox "Character:" & r.Value & " not found" ': Exit For
Info = Split(elems(0).innerText, "| ")
r.Offset(, 1) = Split(Info(2), ChrW(8226))(0)
If InStr(Info(2), ChrW(8226)) Then _
r.Offset(, 2) = Split(Info(2), ChrW(8226))(1)
'// 공격 및 방어력
With XMLhttp
.Open "Get", Range("B2").Value & ENCODEURL(r.Value), False
.setRequestHeader "User-Agent", "Mozilla"
.send
.waitForResponse
JsonStr = .responseText
End With
Set Json = VBAJSON.ParseJson(JsonStr)
r.Offset(, 3) = Json("records")("total_ability")("attack_power_value")
r.Offset(, 4) = Json("records")("total_ability")("max_hp")
'// 장비
With XMLhttp
.Open "Get", Range("B3").Value & ENCODEURL(r.Value), False
.setRequestHeader "User-Agent", "Mozilla"
.send
.waitForResponse
JsonStr = .responseText
End With
Set Json = VBAJSON.ParseJson(JsonStr)
For Each J In Json("records")
If J("equipped_part") Like "hand" Then
r.Offset(, 5) = J("item")("name")
ElseIf J("equipped_part") Like "finger*" Then
r.Offset(, 6) = J("item")("name")
ElseIf J("equipped_part") Like "ear*" Then
r.Offset(, 7) = J("item")("name")
ElseIf J("equipped_part") Like "neck" Then
r.Offset(, 8) = J("item")("name")
ElseIf J("equipped_part") Like "bracelet" Then
r.Offset(, 9) = J("item")("name")
ElseIf J("equipped_part") Like "belt" Then
r.Offset(, 10) = J("item")("name")
ElseIf J("equipped_part") Like "gloves" Then
r.Offset(, 11) = J("item")("name")
ElseIf J("equipped_part") Like "soul" Then
r.Offset(, 12) = J("item")("name")
ElseIf J("equipped_part") Like "soul_2" Then
r.Offset(, 13) = J("item")("name")
ElseIf J("equipped_part") Like "pet" Then
r.Offset(, 14) = J("item")("name")
ElseIf J("equipped_part") Like "nova" Then
r.Offset(, 15) = J("item")("name")
ElseIf J("equipped_part") Like "soul_badge" Then
r.Offset(, 16) = J("item")("name")
ElseIf J("equipped_part") Like "swift_badge" Then
r.Offset(, 17) = J("item")("name")
ElseIf J("equipped_part") Like "body" Then
r.Offset(, 18) = J("item")("name")
ElseIf J("equipped_part") Like "head" Then
r.Offset(, 19) = J("item")("name")
ElseIf J("equipped_part") Like "eye" Then
r.Offset(, 20) = J("item")("name")
End If
Next J
'진행율 상태바에 표시
Application.StatusBar = r.Row - 4 & " of " & lastRow - 4 & " : " _
& Format((r.Row - 4) * 100 / (lastRow - 4), "00.00") & "% done..."
Next r
Range("A5:V" & lastRow).Columns.AutoFit
Application.ScreenUpdating = True
Application.StatusBar = False
DONE:
Set Json = Nothing
Set XMLhttp = Nothing
Set Html = Nothing
End Sub
Function ENCODEURL(varText As Variant, Optional blnEncode = True)
Static objHtmlfile As Object
If objHtmlfile Is Nothing Then
Set objHtmlfile = CreateObject("htmlfile")
With objHtmlfile.parentWindow
.execScript "function encode(s) {return encodeURIComponent(s)}", "jscript"
End With
End If
If blnEncode Then
ENCODEURL = objHtmlfile.parentWindow.encode(varText)
End If
End Function
최근댓글