Excel VBA, Web Crolling, 주가 가져오기 v4

 Sub stock_v4()
'v1 today stock price
'v2 timer
'v3 exchange rate
'v4 PBR, WTI

    Dim ie As InternetExplorer
    Dim strURL As String
    Dim i As Integer
    Dim start_time As Single
    Dim end_time As Single
    Dim wti_temp_1 As String
    Dim wti_temp_2 As String
    Dim wti As Variant
    
    start_time = Timer
    
    Range(Range("D3"), Range("D3").End(xlDown)).ClearContents
    For i = 3 To Range("C1000").End(xlUp).Row
        Set ie = CreateObject("InternetExplorer.application")
        strURL = "https://finance.naver.com/item/coinfo.nhn?code=" & Range("C" & i)
        ie.Navigate strURL
        ie.Visible = False
        Do While (ie.ReadyState <> READYSTATE_COMPLETE Or ie.Busy = True)
            DoEvents
        Loop
        Application.Wait (Now + TimeValue("00:00:01"))  '1초 대기
        Range("D" & i).Value = ie.document.getElementById("nowVal_td_0").innerText    'today stock price
        Range("H" & i).Value = ie.document.getElementById("_pbr").innerText '리츠의 경우 PBR 검색 안 됨
        ie.Quit
        Set ie = Nothing
    Next i
    
    Set ie = CreateObject("InternetExplorer.application")
    strURL = "https://search.naver.com/search.naver?where=nexearch&sm=top_hty&fbm=1&ie=utf8&query=%ED%99%98%EC%9C%A8"
    ie.Navigate strURL
    ie.Visible = False
    Do While (ie.ReadyState <> READYSTATE_COMPLETE Or ie.Busy = True)
        DoEvents
    Loop
    Range("A1").Value = "환율: " & ie.document.getElementsByTagName("strong")(16).innerText & vbLf & "(하나은행 기준)"
    ie.Quit
    Set ie = Nothing
    
    Set ie = CreateObject("InternetExplorer.application")
    strURL = "https://search.naver.com/search.naver?where=nexearch&sm=top_hty&fbm=1&ie=utf8&query=wti"
    ie.Navigate strURL
    ie.Visible = False
    Do While (ie.ReadyState <> READYSTATE_COMPLETE Or ie.Busy = True)
        DoEvents
    Loop
    Application.Wait (Now + TimeValue("00:00:01"))
    wti_temp_1 = ie.document.getElementsByClassName("spt_con").Item.innerText
    wti_temp_2 = Right(wti_temp_1, Len(wti_temp_1) - 2)
    wti = Split(wti_temp_2, " ")
    Range("B1").Value = "유가(WTI)" & vbLf & wti(0) & " " & wti(4)
    ie.Quit
    Set ie = Nothing
    
    end_time = Timer
    MsgBox Round(end_time - start_time) & "s elapsed."
    
End Sub

댓글

이 블로그의 인기 게시물

<배당주 투자> 배당주(은행) '22년 8월

은행주 - 배당주 투자 일기, 2022.07.04(월)

<배당주 투자> 지주사, '22년 8월