Я согласен с комментарием, что RSeleneium будет лучшим для этого. Вот ваш желаемый результат с использованием RSelenium.
library(RSelenium)
url <- "https://www.distancecalculator.net/"
#Start Selenium
rD <- rsDriver(port = 4444L, browser = "chrome")
remDr <- rD$client
remDr$navigate(url)
#Type in the information
originCity <- remDr$findElement(using = "css", "#originCity")
originCity$sendKeysToElement(list("Craíbas - AL, Brasil"))
#Click the first item
clickFirst <- remDr$findElements(using = "css", ".pac-item")
clickFirst2 <- unlist(lapply(clickFirst, function(x){
x$getElementText()
}))
clickFirst2
click <- clickFirst[[which(clickFirst2 == clickFirst2[1])]]
click$clickElement()
destinationCity <- remDr$findElement(using = "css", "#destinationCity")
destinationCity$sendKeysToElement(list("Maceió - AL, Brasil"))
#Click the first item
clickFirst <- remDr$findElements(using = "css", ".pac-item")
clickFirst2 <- unlist(lapply(clickFirst, function(x){
x$getElementText()
}))
clickFirst2
click <- clickFirst[[which(clickFirst2 == clickFirst2[1])]]
click$clickElement()
#No longer Necessary
calculate <- remDr$findElements(using = "xpath", '//*[contains(concat( " ",
@class, " " ), concat( " ", "button", " " ))]')
calculate2 <- unlist(lapply(calculate, function(x){
x$getElementText()
}))
calculate2
click <- calculate[[which(calculate2 == calculate2[1])]]
click$clickElement()
#Scrape the result
dist <- remDr$findElements(using = "css", "#driving-distance-km")
dist <- unlist(lapply(dist, function(x){
x$getElementText()
}))
dist
remDr$close()
И ссылка на информацию о пакете RSelenium: https://cran.r -project.org / web / packages / RSelenium / виньетки / RSelenium-basics.html