可以获取初始页面的链接文本:
library(RSelenium)
library(rvest)
shell('docker run -d -p 4445:4444 selenium/standalone-firefox')
remDr <- remoteDriver(remoteServerAddr = "localhost", port = 4445L, browserName = "firefox")
remDr$open()
remDr$navigate("https://www.ecb.europa.eu/press/pressconf/html/index.en.html")
# This is useful to load all the page
for(i in 1 : 100)
{
print(i)
remDr$executeScript(paste0("scroll(0, ", i * 2000, ")"))
}
Sys.sleep(5)
html_Content <- remDr$getPageSource()[[1]]
html_Link <- str_extract_all(string = html_Content, pattern = "/press/pressconf/[^<]*html")[[1]]
html_Link_En <- html_Link[str_detect(html_Link, "\\.en\\.html")]
links_To_Remove <- c("/press/pressconf/html/index.en.html", "/press/pressconf/visual-mps/html/index.en.html" )
html_Link_En <- html_Link_En[!(html_Link_En %in% links_To_Remove)]
html_Link_En <- unique(html_Link_En)
# Extract text from first link
# It is possible to use a for loop to get the text of all links ...
html_Content <- read_html(paste0("https://www.ecb.europa.eu", html_Link_En[1]))
html_Content %>% html_text()