您需要“RSelenum”来执行无头导航。
检查设置:
How to set up rselenium for R?
library(RSelenium)
library(rvest)
library(tidyvers)
url="https://metro.zakaz.ua/uk/?promotion=1"
rD <- rsDriver(port=4444L, browser="chrome")
remDr <- rD[['client']]
remDr$navigate(url)
### adjust items you want to scrape
src <- remDr$getPageSource()[[1]]
pg <- read_html(src)
tbl <- tibble(
product_name = pg %>% html_nodes(".product-card-name") %>% html_text(),
product_info = pg %>% html_nodes(".product-card-info") %>% html_text()
)
## to handle pagenation (tested with 5 pages) - adjust accordinly
for (i in 2:5) {
pages <- remDr$findElement(using = 'css selector',str_c(".page:nth-child(",i,")"))
pages$clickElement()
## wait 5 sec to load
Sys.sleep(5)
src <- remDr$getPageSource()[[1]]
pg <- read_html(src)
data <- tibble(
product_name = pg %>% html_nodes(".product-card-name") %>% html_text(),
product_info = pg %>% html_nodes(".product-card-info") %>% html_text()
)
tbl <- tbl %>% bind_rows(data)
}
nrow(tbl)
head(tbl)
tail(tbl)
下面是一个快速输出:
Output