Skip to content

Instantly share code, notes, and snippets.

@Kyeongrok
Created July 13, 2017 16:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kyeongrok/fed1833139fd46a6cfb3f73524a08844 to your computer and use it in GitHub Desktop.
Save Kyeongrok/fed1833139fd46a6cfb3f73524a08844 to your computer and use it in GitHub Desktop.
Private 인터넷익스플로러 As InternetExplorer
Sub main()
    'IE 객체 작성
    Set 인터넷익스플로러 = CreateObject("InternetExplorer.Application")
    검색어 = WorksheetFunction.EncodeURL("맛집")
    페이지주소 = "https://search.naver.com/search.naver?where=nexearch&sm=top_hty&fbm=1&ie=utf8&query=" & 검색어

    인터넷익스플로러.Visible = True
    인터넷익스플로러.Navigate2 (페이지주소)
    
    '읽기 완료 대기
    While 인터넷익스플로러.ReadyState <> READYSTATE_COMPLETE Or 인터넷익스플로러.Busy = True
        DoEvents
        Application.Wait (Now + TimeValue("0:00:01"))
    Wend
    Application.Wait (Now + TimeValue("0:00:01"))

    '엑셀 시트에 검색 결과 출력하기
    Row = 1
    For Each element In 인터넷익스플로러.Document.getElementsByClassName("sh_blog_title ")
        Worksheets(1).Cells(Row, 1) = Row
        Worksheets(1).Cells(Row, 2) = element.innerText
        Row = Row + 1
    Next element
    인터넷익스플로러.Quit
    Set 인터넷익스플로러 = Nothing
End Sub

@Reflective-light
Copy link

안녕하세요.
저 책을 구매하고, 돌렸는데 실행이 되지 않아 문의 드립니다. 검색어, 페이지주소등이 미리 선언되지 않았는데, 실행이 되나요?
코드 보도 이해가 되지 않았는데..실행해 보니 역시 검색어에서 오류가 생기더라고요.

@Kyeongrok
Copy link
Author

실행 했을 때 어떤 에러가 나는지 알 수 있을까요?

@Reflective-light
Copy link

네 우선 검색어에 Highlight되구요, 팝업에는 Compile error: Variable not defined라고 되어요.
음.. 제가 초보라서 그렇지만, 아는 지식안에서는 미리 검색어랑 페이지 주소가 미리 string이나 기타 개체로 선언되어야 되는거 아닌가 해서요.

@Kyeongrok
Copy link
Author

혹시 Option Explicit을 쓰셨나요? Variable not defined 이 에러는 해당 변수가 없을때 나는데 보통은 변수가 처음 생성 될 때 자동으로 선언을 해주거든요

@Reflective-light
Copy link

아...네, Option Explicit 사용하고 있습니다. 생성될때 자동으로 생성도 되나봐요. 음. 제꺼는 Option Explicit 제거하고, 변수선언요구도 tick off 하고, 종료 후 다시 오픈해도 계속 같은 오류가 나오네요. 2010버전이라 그런걸까요? 음..ㅠ string 이랑 object로 해 봤는데 안되네요. 혹시 어떤 개체로 선언해야 하는거에요? 검색어랑, 페이지 주소요.

@Kyeongrok
Copy link
Author

Option Explicit 제거 했는데도 Variable not defined가 나나요? 아니면 다른 에러가 나나요?

@Reflective-light
Copy link

네, 같은 에러가 나오고 있습니다.

우선은 검색어 변수에 나오고 있습니다. 페이지주소 변수에도 나올 것 같습니다.

@Reflective-light
Copy link

앗 죄송해요. 다시 실행하니 아래와 같은 메시지가 나타납니다.
Object doesn't support this property or method.
검색어 = WorksheetFunction.EncodeURL("맛집") 전체가 하이라이트 되구요.

@Reflective-light
Copy link

해결했어요. 2013부터는 ENCODERUL을 제공한다고 해요. 저는 안되어서 관련 FUNCTION을 추가해주어야 하나봐요.
감사합니다.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment