###경제빅데이터 분석 14주차 수업 내용 중
우선 필요한 패키지들을 로딩한 후 cmd를 작업관리자 모드로 실행한 후 R로 chrome을 제어할 수 있도록 준비한다.
library(RSelenium)
library(rvest)
library(stringr)
remDr <- remoteDriver(
remoteServerAddr = "localhost",
port = 4445L,
browserName ='chrome')
remDr$open()
## [1] "Connecting to remote server"
## $acceptInsecureCerts
## [1] FALSE
##
## $browserName
## [1] "chrome"
##
## $browserVersion
## [1] "86.0.4240.198"
##
## $chrome
## $chrome$chromedriverVersion
## [1] "86.0.4240.22 (398b0743353ff36fb1b82468f63a3a93b4e2e89e-refs/branch-heads/4240@{#378})"
##
## $chrome$userDataDir
## [1] "C:\\Users\\UserK\\AppData\\Local\\Temp\\scoped_dir12524_1884102709"
##
##
## $`goog:chromeOptions`
## $`goog:chromeOptions`$debuggerAddress
## [1] "localhost:12261"
##
##
## $networkConnectionEnabled
## [1] FALSE
##
## $pageLoadStrategy
## [1] "normal"
##
## $platformName
## [1] "windows"
##
## $proxy
## named list()
##
## $setWindowRect
## [1] TRUE
##
## $strictFileInteractability
## [1] FALSE
##
## $timeouts
## $timeouts$implicit
## [1] 0
##
## $timeouts$pageLoad
## [1] 300000
##
## $timeouts$script
## [1] 30000
##
##
## $unhandledPromptBehavior
## [1] "dismiss and notify"
##
## $`webauthn:virtualAuthenticators`
## [1] TRUE
##
## $webdriver.remote.sessionid
## [1] "ecc9821746901ca9ee8e7c70c5c9223f"
##
## $id
## [1] "ecc9821746901ca9ee8e7c70c5c9223f"
remoteDriver함수를 통해 chrome을 제어하는 함수를 remDr에 할당한 후 이를 실행시킨다.
remDr$navigate("https://www.naver.com")
webElem1 <- remDr$findElement(using = 'class',
value = 'input_text')
webElem1$highlightElement()
webElem1$clearElement()
webElem1$sendKeysToElement(list('네이버 맞춤법 검사기', key = 'enter'))
chrome의 개발자 모드를 통해 검색창의 위치 (class, inlut_text)를 파악한 후 이를 webElem1에 할당한다.
이 위치를 깜빡이게 하여 다시 확인한 후, 검색창을 한 차례 비운다.
또한 검색창에 네이버 맞춤법 검사기라 입력한 후 enter를 작동시켜 이를 검색하게 한다.
webElem2 <- remDr$findElement(using = 'class',
value = 'txt_gray')
webElem2$highlightElement()
webElem2$clearElement()
sentence <- list('과제가너무많아서화가난다')
webElem2$sendKeysToElement(sentence)
우선 맞춤법 검사기의 위치(class, txt_gray)를 찾아 할당하고, 이 위치를 다시 확인한 후 비워준다.
또 ‘과제가너무많아서화가난다’라는 list를 만들어준 후 sendKeyToElement()를 통해 맞춤법 검색 박스 안에 list를 입력한다.
webElem3 <- remDr$findElement(using = 'class',
value = 'btn_check')
webElem3$highlightElement()
webElem3$clickElement()
검사하기 버튼의 위치(class,btn_check)를 할당한 후 위치를 재확인하고 clickElement()를 통해 실행시킨다.
webElem4 <- remDr$findElement(using = 'class',
value = '_result_text')
webElem4$highlightElement()
webElem4$getElementText() -> result
result_clean <- stringr::str_split(result,"\\.")
result_clean
## [[1]]
## [1] "과제가 너무 많아서 화가 난다"
검사 결과의 위치를 할당한 후, 결과 값을 할당한다. stringr 패키지의 str_split(result,“\.”)를 통해 문자열을 분리한다.
\[**전체 실행**\]
remDr <- remoteDriver(
remoteServerAddr = "localhost",
port = 4445L,
browserName = "chrome"
)
remDr$open()
## [1] "Connecting to remote server"
## $acceptInsecureCerts
## [1] FALSE
##
## $browserName
## [1] "chrome"
##
## $browserVersion
## [1] "86.0.4240.198"
##
## $chrome
## $chrome$chromedriverVersion
## [1] "86.0.4240.22 (398b0743353ff36fb1b82468f63a3a93b4e2e89e-refs/branch-heads/4240@{#378})"
##
## $chrome$userDataDir
## [1] "C:\\Users\\UserK\\AppData\\Local\\Temp\\scoped_dir16872_1498368799"
##
##
## $`goog:chromeOptions`
## $`goog:chromeOptions`$debuggerAddress
## [1] "localhost:12328"
##
##
## $networkConnectionEnabled
## [1] FALSE
##
## $pageLoadStrategy
## [1] "normal"
##
## $platformName
## [1] "windows"
##
## $proxy
## named list()
##
## $setWindowRect
## [1] TRUE
##
## $strictFileInteractability
## [1] FALSE
##
## $timeouts
## $timeouts$implicit
## [1] 0
##
## $timeouts$pageLoad
## [1] 300000
##
## $timeouts$script
## [1] 30000
##
##
## $unhandledPromptBehavior
## [1] "dismiss and notify"
##
## $`webauthn:virtualAuthenticators`
## [1] TRUE
##
## $webdriver.remote.sessionid
## [1] "588b52c8b0c6371482ad737d5a7d8cc2"
##
## $id
## [1] "588b52c8b0c6371482ad737d5a7d8cc2"
remDr$navigate("https://www.naver.com")
webElem1 <- remDr$findElement(using = 'class', value = 'input_text')
webElem1$highlightElement()
webElem1$clearElement()
webElem1$sendKeysToElement(list('네이버 맞춤법 검사기',key = 'enter'))
sentence <- list("과제가너무많아서화가난다")
webElem2 <- remDr$findElement(using = 'class', value = 'txt_gray')
webElem2$highlightElement()
webElem2$clearElement()
webElem2$sendKeysToElement(sentence)
Sys.sleep(2)
webElem3 <- remDr$findElement(using = 'class', value = 'btn_check')
webElem3$highlightElement()
webElem3$clickElement()
Sys.sleep(2)
webElem4 <- remDr$findElement(using = 'class', value = '_result_text')
webElem4$highlightElement()
webElem4$getElementText() -> result
result_clean <- stringr::str_split(result, "\\.")
result_clean
## [[1]]
## [1] "과제가 너무 많아서 화가 난다"