r-如何检测错误:此页面没有正确加载谷歌地图



假设我有一个网站要不时检查,看看它是否正常工作,我想在R.中编码的帮助下自动完成

举个例子,我可能每小时做一次以下操作,检查它是否会给我404 Not Found错误。

library(httr)
r <- GET("http://httpbin.org/status/404")
http_error(r)
[1] TRUE
status_code(r)
[1] 404

但我的网页使用谷歌地图,有时我会检测到以下错误:

此页面未正确加载谷歌地图

有人知道如何在不需要浏览网页的情况下,以与上述相同的方式检测此类错误吗?

您需要两个脚本。下面给出的第一个检查是否在的情况下此页面没有正确加载谷歌地图,使用通知程序包打印错误消息。有关RSelenium的基础知识,请咨询https://cran.r-project.org/web/packages/RSelenium/vignettes/basics.html.据说有三种方法可以运行Selenium服务器。我选择了最容易的,也就是第二个:rsDriver。

# To get the message written I use the notifier package, 
# https://github.com/gaborcsardi/notifier 
library(notifier)
# To do the webscraping I use RSelenium 
library(RSelenium)
# To check whether the string contains what I am after I prefer stringr
library(stringr)

rD <- rsDriver(verbose = FALSE)
remDr <- rD$client
remDr$navigate("https:...")
# Wait a little while it is busy with downloading everything
Sys.sleep(120)
# we scrape it and convert it to a character string 
a <- XML::htmlParse(remDr$getPageSource()[[1]])
b <- as(a, "character")
# we check if the string has the error phrase.
result <- str_detect(b, "This page can't load Google Maps correctly")
# if yes, then the following error message is printed.

if (result == TRUE){notify(title = "ERROR",
msg = sprintf("This page can't load Google Maps correctly"))}

# to close the client and the server
remDr$close()
rD$server$stop()

下面是第二个使用taskscheduler包在特定时间点从R本身自动执行的脚本。在这种情况下,代码每5分钟执行一次。

library(taskscheduleR)
myscript <- "the place of the first script"
taskscheduler_create(taskname = "myfancyscript_5min", rscript = myscript,
schedule = "MINUTE", starttime = "09:10", modifier = 5)

最新更新