Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用R向下滚动进行网页刮取_R_Web Scraping - Fatal编程技术网

使用R向下滚动进行网页刮取

使用R向下滚动进行网页刮取,r,web-scraping,R,Web Scraping,我希望从以下URL下载前两列(“开始的天然气日”和“储存中的天然气”): 默认期间设置为“上个月”,我需要“全部” 有人能告诉我我可以使用什么软件包来完成这类任务吗? 还有一个免费的API,但我也没能做到这一点 感谢您的每一个意见! 非常感谢!让我们试着让您更接近API路径。如果您有API密钥,您可以(但不应该)将其直接传递给以下函数。您应该将其作为以下内容放入~/.Renviron中: AGSI_KEY=thekeytheygaveyou 然后重新启动你的R会话。它将被自动使用 以下函数采

我希望从以下URL下载前两列(“开始的天然气日”和“储存中的天然气”):

默认期间设置为“上个月”,我需要“全部”

有人能告诉我我可以使用什么软件包来完成这类任务吗? 还有一个免费的API,但我也没能做到这一点

感谢您的每一个意见!
非常感谢!

让我们试着让您更接近API路径。如果您有API密钥,您可以(但不应该)将其直接传递给以下函数。您应该将其作为以下内容放入
~/.Renviron
中:

AGSI_KEY=thekeytheygaveyou
然后重新启动你的R会话。它将被自动使用

以下函数采用开始/结束日期

get_agsi_data <- function(start, end, agsi_api_key = Sys.getenv("AGSI_KEY")) {

  start[1] <- as.character(as.Date(start[1]))
  end[1] <- as.character(as.Date(end)[1])

  httr::GET(
    url = "https://agsi.gie.eu/api/data/eu", # NOTE THE HARDCODING FOR eu
    httr::add_headers(`x-key` = agsi_api_key),
    httr::user_agent("user@example.com") # REPLACE THIS WITH YOUR EMAIL ADDRESS
  ) -> res

  httr::stop_for_status(res) # warns when API issues

  out <- httr::content(res, as = "text", encoding = "UTF-8")

  out <- jsonlite::fromJSON(out)

  sapply(out$info, function(x) { # the info element is an ugly list so we need to make it better
    if (length(x)) {
      x <- paste0(x, collapse = "; ") 
    } else {
      NA_character_
    }
  }) -> info

  out$info <- info

  readr::type_convert(
    df = out,
    col_types = cols(
      status = col_character(),
      gasDayStartedOn = col_date(format = ""),
      gasInStorage = col_double(),
      full = col_double(),
      trend = col_double(),
      injection = col_double(),
      withdrawal = col_double(),
      workingGasVolume = col_double(),
      injectionCapacity = col_double(),
      withdrawalCapacity = col_double()
    )
  ) -> out

  class(out) <- c("tbl_df", "tbl", "data.frame")

  out

}

xdf <- get_agsi_data("2018-06-01", "2018-10-01")

xdf
## # A tibble: 2,880 x 11
##    status gasDayStartedOn gasInStorage  full trend injection withdrawal workingGasVolume injectionCapacity
##  * <chr>  <date>                 <dbl> <dbl> <dbl>     <dbl>      <dbl>            <dbl>             <dbl>
##  1 E      2018-11-19              918.  86.1 -0.41      343.      4762.            1067.            11469.
##  2 E      2018-11-18              923.  86.5 -0.22      534.      2841.            1067.            11469.
##  3 E      2018-11-17              925.  86.7 -0.2       649.      2796.            1067.            11469.
##  4 E      2018-11-16              927.  86.9 -0.24      492.      3014.            1067.            11469.
##  5 E      2018-11-15              930.  87.1 -0.16      503.      2210.            1067.            11469.
##  6 E      2018-11-14              931.  87.3 -0.1       605.      1682.            1067.            11469.
##  7 E      2018-11-13              933.  87.4 -0.07      651.      1438.            1067.            11469.
##  8 E      2018-11-12              933.  87.5 -0.05      833.      1391.            1067.            11468.
##  9 E      2018-11-11              934.  87.5  0.09     1607.       659.            1067.            11478.
## 10 E      2018-11-10              933.  87.4  0.06     1458.       796.            1067.            11478.
## # ... with 2,870 more rows, and 2 more variables: withdrawalCapacity <dbl>, info <chr>

get\u agsi\u数据使用selenium,但使用API更简单不需要其他下载谢谢!为了确保我理解您的意思,如果我使用API,我不需要任何软件包?再次感谢!您需要
jsonlite
Library非常感谢您的帮助!尽管我在运行代码时收到以下错误消息:error in readr::type\u convert(df=out,col_types=cols(status=col_character(),:is.data.frame(df)不是真的我也注意到了一些东西..在第18行之后-out是的。这意味着你没有指定API键或者API键不正确。很奇怪..我使用了以下结构库(“readr”)AGSI_KEY=“0bf8e8a083be3e9326eb0dd04e68fxxx”#修改后的KEY get_AGSI_data噢,我似乎错过了一个重要的步骤。我刚在脚本开始时运行了关键行..现在熟悉了Renviron。再次感谢您的耐心:)