At this link (https://portraits.ouranos.ca/fr/spatial?a=0&c=0&discrete=1&e=CMIP6&i=tg_mean&p=50&r=mrc001&s=annual&scen=ssp370&w=0&yr=2071) there is a tag (mrc001) that matches the name of a MRC such as the one in the picture (Abitibi). The website uses Javascript and I think I need to use RSelenium for this.
However, I got
Error in checkError(res) :
Undefined error in httr call. httr output: Failed to connect to localhost port 4545 after 0 ms: Couldn't connect to server
when I use this script :
library(RSelenium)
library(rvest)
# Function to extract the region name based on the URL
extract_region_name <- function(url) {
ddd = remoteDriver(browserName = 'chrome', port = 4545L)
ddd$open()
# Navigate to the URL
ddd$navigate(url)
# Wait for the page to load
Sys.sleep(5)
# Extract the page source
page_source <- ddd$getPageSource()[[1]]
# Parse the HTML content
webpage <- read_html(page_source)
# Extract the region name using CSS selectors
region_name <- webpage %>%
html_nodes("button.region-title h2") %>%
html_text(trim = TRUE)
# Clean up the extracted text to remove any extra characters
region_name <- gsub("\\s+\\<i.*", "", region_name)
# Close RSelenium server and browser
ddd$close()
ddd$stop()
return(region_name)
}
# Example URLs
url1 <- "https://portraits.ouranos.ca/fr/spatial?a=0&c=0&discrete=1&e=CMIP6&i=tg_mean&p=50&r=mrc001&s=annual&scen=ssp370&w=0&yr=2071"
# Extract region names from the URLs
region_name1 <- extract_region_name(url1)
I've tried using the system sleep command without luck. Error in checkError(res) : Undefined error in httr call. httr output: Failed to connect to localhost port 4445: Connection refused
I've also tried
rD <- rsDriver(browser="chrome", port=4545L, chromever = NULL)
which gave me
checking Selenium Server versions:
BEGIN: PREDOWNLOAD
BEGIN: DOWNLOAD
BEGIN: POSTDOWNLOAD
checking geckodriver versions:
BEGIN: PREDOWNLOAD
BEGIN: DOWNLOAD
BEGIN: POSTDOWNLOAD
checking phantomjs versions:
BEGIN: PREDOWNLOAD
BEGIN: DOWNLOAD
BEGIN: POSTDOWNLOAD
[1] "Connecting to remote server"
Could not open chrome browser.
Client error message:
Undefined error in httr call. httr output: Failed to connect to localhost port 4545 after 0 ms: Couldn't connect to server
Check server log for further details.
Warning message:
In rsDriver(browser = "chrome", port = 4545L, chromever = NULL) :
Could not determine server status.
I want to programmatically get the numbers (i.e., mrc001) and the name (i.e., Abitibi) in a data.frame and loop through each mrcXXX number with
sprintf("https://portraits.ouranos.ca/fr/spatial?a=0&c=0&discrete=1&e=CMIP6&i=tg_mean&p=50&r=mrc%03d&s=annual&scen=ssp370&w=0&yr=2071" ,
1:104)
The page is very open with it's api fetch requests - you can simply do
resp <- httr::GET("https://pavics.ouranos.ca/pc-geoserver/pc/ows?service=WFS&version=1.0.0&request=GetFeature&typeName=pc:region_mrc_mid&outputFormat=application%2Fjson")
res <- jsonlite::fromJSON(rawToChar(resp$content))
df <- data.frame(
id = res$features$properties$id,
region = res$features$properties$name
)
id | region |
---|---|
1 | Abitibi |
2 | and so forth |