이미지 끌어오기2

library(httr)
library(rvest)
library(RSelenium)
## Loading required package: RCurl
## Loading required package: bitops
## Loading required package: RJSONIO
## Loading required package: XML
## 
## Attaching package: 'XML'
## 
## The following object is masked from 'package:rvest':
## 
##     xml
checkForServer()
startServer()

firefox <- remoteDriver(browserName = 'firefox')
firefox$open()
## [1] "Connecting to remote server"
## $applicationCacheEnabled
## [1] TRUE
## 
## $rotatable
## [1] FALSE
## 
## $handlesAlerts
## [1] TRUE
## 
## $databaseEnabled
## [1] TRUE
## 
## $version
## [1] "39.0"
## 
## $platform
## [1] "WINDOWS"
## 
## $nativeEvents
## [1] FALSE
## 
## $acceptSslCerts
## [1] TRUE
## 
## $webdriver.remote.sessionid
## [1] "5aa76896-69ee-42fd-92ad-688b9d937cb7"
## 
## $webStorageEnabled
## [1] TRUE
## 
## $locationContextEnabled
## [1] TRUE
## 
## $browserName
## [1] "firefox"
## 
## $takesScreenshot
## [1] TRUE
## 
## $javascriptEnabled
## [1] TRUE
## 
## $cssSelectorsEnabled
## [1] TRUE
## 
## $id
## [1] "5aa76896-69ee-42fd-92ad-688b9d937cb7"
url <- 'http://m.webtoon.daum.net/m/webtoon/view/damageovertime#page_no=6'

firefox$navigate(url)

src <- firefox$getPageSource()[[1]]
h <- html(src)

h <- html_nodes(h, 'div.list_comm ul li a')
img.url <- html_attr(h, 'href')     

firefox$navigate(paste('http://m.webtoon.daum.net', img.url[1], sep=''))

src2 <- firefox$getPageSource()[[1]]
h2 <- html(src2)

h2 <- html_nodes(h2, 'div.layer_body img')
toons.url <- html_attr(h2, 'src')

for (i in 1:length(toons.url))
{
  toons.result <- GET(toons.url[i])
  writeBin(content(toons.result, 'raw'), sprintf('%02d.jpg', i))
}